Professional Documents
Culture Documents
An array is defined as the collection of similar type of data items stored at contiguous
memory locations. Arrays are the derived data type in C programming language which
can store the primitive type of data such as int, char, double, float, etc. It also has the
capability to store the collection of derived data types, such as pointers, structure, etc.
The array is the simplest data structure where each data element can be randomly
accessed by using its index number.
C array is beneficial if you have to store similar elements. For example, if we want to
store the marks of a student in 6 subjects, then we don't need to define different
variables for the marks in the different subject. Instead of that, we can define an array
which can store the marks in each subject at the contiguous memory locations.
By using the array, we can access the elements easily. Only a few lines of code are
required to access the elements of the array.
Properties of Array
The array contains the following properties.
o Each element of an array is of same data type and carries the same size, i.e., int
= 4 bytes.
o Elements of the array are stored at contiguous memory locations where the first
element is stored at the smallest memory location.
o Elements of the array can be randomly accessed since we can calculate the
address of each element of the array with the given base address and the size
of the data element.
Advantage of C Array
1) Code Optimization: Less code to the access the data.
PROGRAMMING FOR PROBLEM SOLVING UNIT 3
2) Ease of traversing: By using the for loop, we can retrieve the elements of an array
easily.
3) Ease of sorting: To sort the elements of the array, we need a few lines of code only.
4) Random Access: We can access any element randomly using the array.
Disadvantage of C Array
1) Fixed Size: Whatever size, we define at the time of declaration of the array, we can't
exceed the limit. So, it doesn't grow the size dynamically like LinkedList which we will
learn later.
Declaration of C Array
We can declare an array in the c language in the following way.
1. data_type array_name[array_size];
1. int marks[5];
Here, int is the data_type, marks are the array_name, and 5 is the array_size.
Initialization of C Array
The simplest way to initialize an array is by using the index of each element. We can
initialize each element of the array by using the index. Consider the following example.
1. marks[0]=80;//initialization of array
2. marks[1]=60;
3. marks[2]=70;
4. marks[3]=85;
5. marks[4]=75;
PROGRAMMING FOR PROBLEM SOLVING UNIT 3
C array example
#include<stdio.h>
int main(){
int i=0;
int marks[5];//declaration of array
marks[0]=80;//initialization of array
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;
//traversal of array
for(i=0;i<5;i++){
printf("%d \n",marks[i]);
}//end of for loop
return 0;
}
Output
80
60
70
85
75
int marks[5]={20,30,40,50,60};
In such case, there is no requirement to define the size. So it may also be written
as the following code.
Example program
int marks[]={20,30,40,50,60};
#include<stdio.h>
int main()
{
int i=0;
PROGRAMMING FOR PROBLEM SOLVING UNIT 3
int marks[5]={20,30,40,50,60};//declaration and initialization of array
//traversal of array
for(i=0;i<5;i++)
{
printf("%d \n",marks[i]);
}
return 0;
}
Output
20
30
40
50
60
#include<stdio.h>
#include<conio.h>
void main ()
{
Int a[10],i;
Printf(“enter the 10 values”);
for(i=0;i<10;i++)
Scanf(“%d”,&a[i]);
Printf(“the given values are”);
for(i=0;i<10;i++)
Scanf(“%3d”,a[i]);
}
2./* Program to calculate the sum and average of six subject marks using
arrays */
void main()
{
int a[10],i,sum=0; float avg;
clrscr();
printf("Enter 6 subject marks :");
for(i=0; i<6;i++)
{
PROGRAMMING FOR PROBLEM SOLVING UNIT 3
Scanf(“%d”,&a[i]);
Sum=sum+a[i];
}
Printf(“sum=%d”,sum);
Avg=sum/6;
Printf(“avg=%f”,avg);
}
3./* Program to calculate maximum and minimum of given numbers */
void main()
{
int a[100],n,max=0,min,i;
printf("Enter total numbers :");
scanf("%d",&n);
printf("Enter %d numbers :",n);
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
min = max = a[0];
for(i=1; i<n; i++)
{
if(max<a[i])
max = a[i];
}
printf("Maximum Number = %d",max);
min=a[0];
for(i=1; i<n; i++)
{
if(min>a[i])
min = a[i];
}
printf("\nMinimum Number = %d",min);
}
#include<stdio.h>
PROGRAMMING FOR PROBLEM SOLVING UNIT 3
int main()
printf("Enter n:");
scanf("%d",&n);
printf("Enter %d elements:",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Element to be searched:");
scanf("%d",&KEY);
for(i=1;i<n;i++)
if(a[i]==KEY)
flag = 1;
break;
if(flag == 0)
PROGRAMMING FOR PROBLEM SOLVING UNIT 3
printf("Element was not found");
return 0;
Declaration:
int a[3][2];
Initialization:
Col0 Col1
Row0 21 22
Row1 23 24
Row2 25 26
The total number of elements in two dimensional array is product of no. of rows and
no. of columns. Example : int a [3][2] ; stores 3x2=6 values conceptually stored in
matrix form.
void main()
{
int a[3][2],i,j;
clrscr();
printf("Enter elements of 3x2 matrix :");
for(i=0; i<3; i++)
{
for(j=0; j<2; j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Given Elements are :\n");
for(i=0; i<3; i++)
{
for(j=0; j<2; j++)
{
printf("%3d",a[i][j]);
}
printf("\n");
}
}
Output:
Enter elements of 3x2 matrix : 1 5 2 6 3 7
Given elements
15
PROGRAMMING FOR PROBLEM SOLVING UNIT 3
26
37
if (n != p)
{
PROGRAMMING FOR PROBLEM SOLVING UNIT 3
printf("Matrix is incompatible for multiplication\n");
}
else
{
printf("Enter the elements of Matrix-A:\n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d", & a[i][j]);
}
}
return 0;
}
Output
Enter the order of first matrix
2
2
Enter the order of second matrix
2
2
Enter the elements of Matrix-A:
2
2
2
2
Enter the elements of Matrix-B:
2
2
2
2
The product of the two matrices is:-
8 8
8 8
/* program to check whether a matrix is an identity matrix or
not */
#include <stdio.h>
// In a square matrix, if all the main diagonal elements are 1's and
// all the remaining elements are 0's, it is called an Identity Matrix.
int main() {
int arr1[10][10];
PROGRAMMING FOR PROBLEM SOLVING UNIT 3
int r1, c1;
int i, j, yn = 1;
return 0;
}
Dynamic arrays
Dynamic arrays are a powerful data structure in programming that allows
for creating and manipulating arrays of varying sizes during runtime. In C,
dynamic arrays are implemented using pointers and memory allocation
functions, making them a valuable tool for optimizing memory usage and
creating efficient programs. In this article, we will explore the concept of
dynamic arrays in C, their advantages and disadvantages, and how to create
and manipulate them.
1. int *arr;
2. int size = 10;
3. arr = (int*) malloc(size * sizeof(int));
C malloc() method
Example
#include <stdio.h>
#include <stdlib.h>
int main()
int* ptr;
int n, i;
scanf("%d",&n);
if (ptr == NULL) {
exit(0);
else {
ptr[i] = i + 1;
return 0;
Output:
2. C calloc() method
Syntax
ptr = (cast-type*)calloc(n, element-size);
here, n is the num of elements and element-size is the size of each element.
For Example:
ptr = (float*) calloc(25, sizeof(float));
This statement allocates contiguous space in memory for 25 elements, each with the
size of the float.
If space is insufficient, allocation fails, and it returns a NULL pointer.
Example
#include <stdio.h>
#include <stdlib.h>
int main() {
int* ptr;
int n, i;
n = 5;
printf("Enter total number of elements present in an array: %d\n", n);
ptr = (int*)calloc(n, sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.\n");
PROGRAMMING FOR PROBLEM SOLVING UNIT 3
exit(0);
}
else {
printf("Memory successfully allocated.\n");
for (i = 0; i < n; ++i) {
ptr[i] = i + 1;
}
printf("The elements in an array are: ");
for (i = 0; i < n; ++i) {
printf("%d, ", ptr[i]);
}
}
return 0;
}
Output
Enter the total number of elements present in an array: 5
Memory successfully allocated.
The elements in an Array are: 1, 2, 3, 4, 5,
3. C free() method
To dynamically de-allocate memory in C, use the "free" technique. Memory allocated
with the malloc() and calloc() procedures is not de-allocated on their own. As a result,
anytime dynamic memory allocation occurs, the free() function is employed. It frees up
memory, which helps to prevent memory waste.
Syntax
free(ptr);
Example
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr, *ptr1;
int n, i;
n = 5;
printf("Enter the total number of elements: %d\n", n);
ptr = (int*)malloc(n * sizeof(int));
ptr1 = (int*)calloc(n, sizeof(int));
// Check if the memory has been successfully
// allocated by malloc or not
if (ptr == NULL || ptr1 == NULL)
PROGRAMMING FOR PROBLEM SOLVING UNIT 3
{
printf("Memory not allocated.\n");
exit(0);
}
else
{
//memory has been successfully allocated
printf("Memory successfully allocated using malloc.\n");
Syntax
ptr = realloc(ptr, newSize);
C example
#include <stdio.h>
PROGRAMMING FOR PROBLEM SOLVING UNIT 3
#include <stdlib.h>
int main() {
int* ptr;
int n, i;
n = 5;
printf("Enter the total number of elements: %d\n", n);
ptr = (int*)calloc(n, sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.\n");
exit(0);
}
else {
printf("Memory successfully allocated using calloc.\n");
for (i = 0; i < n; ++i) {
ptr[i] = i + 1;
}
printf("Elements are: ");
for (i = 0; i < n; ++i) {
printf("%d, ", ptr[i]);
}
n = 10;
printf("\n\nEnter the new size of an array: %d\n", n);
ptr = realloc(ptr, n * sizeof(int));
// Illegal
str = "hello";
Method Description
1. #include<stdio.h>
2. #include <string.h>
PROGRAMMING FOR PROBLEM SOLVING UNIT 3
3. int main(){
4. char ch[20]={'m', 'a', 't', 'r', 'u', 's', 'r', 'i', '\0'};
5. printf("Length of string is: %d",strlen(ch));
6. return 0;
7. }
C Copy String: strcpy()
The strcpy(destination, source) function copies the source string in destination.
1. #include<stdio.h>
2. #include <string.h>
3. int main()
4. {
5. char ch[20]={'m', 'a', 't', 'r', 'u', 's', 'r', 'i', '\0'};
6. char ch2[20];
7. strcpy(ch2,ch);
8. printf("Value of second string is: %s",ch2);
9. return 0;
10. }
C String Concatenation: strcat()
The strcat(first_string, second_string) function concatenates two strings and result is
returned to first_string.
1. #include<stdio.h>
2. #include <string.h>
3. int main(){
4. char ch[10]={'h', 'e', 'l', 'l', 'o', '\0'};
5. char ch2[10]={'c', '\0'};
6. strcat(ch,ch2);
7. printf("Value of first string is: %s",ch);
8. return 0;
9. }
Output:
Here, we are using gets() function which reads string from the console.
1. #include<stdio.h>
2. #include <string.h>
3. int main(){
4. char str1[20],str2[20];
5. printf("Enter 1st string: ");
6. gets(str1);//reads string from console
7. printf("Enter 2nd string: ");
8. gets(str2);
9. if(strcmp(str1,str2)==0)
10. printf("Strings are equal");
11. else
12. printf("Strings are not equal");
13. return 0;
14. }
Output:
1. #include<stdio.h>
2. #include <string.h>
3. int main(){
4. char str[20];
5. printf("Enter string: ");
6. gets(str);//reads string from console
7. printf("String is: %s",str);
8. printf("\nReverse String is: %s",strrev(str));
9. return 0;
PROGRAMMING FOR PROBLEM SOLVING UNIT 3
10. }
Output:
1. #include<stdio.h>
2. #include <string.h>
3. int main(){
4. char str[20];
5. printf("Enter string: ");
6. gets(str);//reads string from console
7. printf("String is: %s",str);
8. printf("\nLower String is: %s",strlwr(str));
9. return 0;
10. }
Output:
1. #include<stdio.h>
2. #include <string.h>
3. int main(){
4. char str[20];
5. printf("Enter string: ");
6. gets(str);//reads string from console
7. printf("String is: %s",str);
8. printf("\nUpper String is: %s",strupr(str));
9. return 0;
PROGRAMMING FOR PROBLEM SOLVING UNIT 3
10. }
Output:
enumerated types. The function type is derived from its return type.
For larger programs, it is not possible to understand all aspects of such programs
without reducing them in to smaller parts. The planning for large programs is as
follows
In top-down design, a program is divided into a main module and its related modules.
Each module in turn is divided into sub modules until the resulting modules are
intrinsic; that is until they are implicitly understood without further division. This
process is known as factoring.
Top-down design is usually done using a visual representation of the modules known
as a structure chart. The structure chart shows the relation between each module and
its submodules. The structure chart is read top-down, left-right. The reading starts
from main module followed by reading of sub-modules of main from left to right.
The main module is called calling module because it has sub modules. The sub
modules are known as called modules. Communication between modules in a
structure chart is allowed only through a calling module. No communication takes
place directly between modules that do not have a calling-called relationship. The
technique used to pas data to a function is known as parameter passing. The
parameters are contained in a list that is a definition of data passed to the function by
the caller.
Moving down and left, we then read Module 1. Module 1 ,Module 2 and Module 3 are
at the same level. The Main Module consists of three sub-modules. Module 1 is further
subdivided into three modules, Module 1a, Module 1b, and Module 1c. To write the
code for Module 1, we need to write code for its three sub-modules.
The main module is called calling module because it has sub modules. The sub
modules are known as called modules. Communication between modules in a
structure chart is allowed only through a calling module. No communication takes
place directly between modules that do not have a calling-called relationship.
The technique used to pas data to a function is known as parameter passing. The
parameters are contained in a list that is a definition of data passed to the function by
the caller.
FUNCTIONS IN C
In C, idea of top-down is done using functions. A C program is made of one or more functions,
one and only one of which be called main. The execution of the program always starts with
main, but it can call other functions to do some part of job.
A function is a self-contained block of code that carries out some specific and well-defined
task.
Advantages of functions:
2. Code Reusability: It provides a way to reuse code that is required in more than one place
in a program. Functions can be reusable in other programs (Files).
3. Protecting data: Used to protect data. Local data in function is available only to function
when it is executing. When the function is not running, the data are not accessible.
4.Reduction of source code The length of the source program can be reduced by using
functions at appropriate places. This factor is critical with microcomputers where memory
space is limited.
5. Easier Debugging It is easy to locate and isolate a faulty function for further investigation.
Function Declaration
PROGRAMMING FOR PROBLEM SOLVING UNIT 3
The function declaration tells the compiler about function name, the data type of the return
value and parameters. The function declaration is also called a function prototype. The
function declaration is performed before the main function or inside the main function or any
other function.
Function Definition
The function definition provides the actual code of that function. The function definition is
also known as the body of the function. The actual task of the function is implemented in
the function definition. That means the actual instructions to be performed by a function are
written in function definition. The actual instructions of a function are written inside the
braces "{ }". The function definition is performed before the main function or after the main
function.
Actual code...
Function Call
The function call tells the compiler when to execute the function definition. When a function
call is executed, the execution control jumps to the function definition where the actual code
gets executed and returns to the same functions call once the execution completes. The
function call is performed inside the main function or any other function or inside the function
itself.
Types of Functions in C
In C Programming Language, based on providing the function definition, functions are divided
These pre-defined functions are known as syatem defined functions. The system defined
The system defined functions are also called as Library Functions or Standard
Functions or Pre-Defined Functions. The implementation of system defined functions is
already defined by the system.
In C, all the system defined functions are defined inside the header
files like stdio.h, conio.h, math.h, string.h etc., For example, the
funtions printf() and scanf() are defined in the header file called stdio.h.
Whenever we use system defined functions in the program, we must include the respective
header file using #include statement. For example, if we use a system defined
function sqrt() in the program, we must include the header file called math.h because the
function sqrt() is defined in math.h.
• System defined functions are implemented in .dll files. (DLL stands for Dynamic Link
Library).
• To use system defined functions the respective header file must be included.
PROGRAMMING FOR PROBLEM SOLVING UNIT 3
User Defined Functions
• In C programming language, users can also create their own functions. The functions
that are created by users are called as user defined functions. The user defined
function is defined as follows...
• The function whose definition is defined by the user is called as user defined
function.
• That means the function that is implemented by user is called as user defined
function. For example, the function main is implemented by user so it is called as user
defined function.
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
int num1, num2, result ;
return a+b ;
addition(int,int)" tells the compiler that there is a function with name addition which takes
two integer values as parameters and returns an integer value. The function call statement
takes the execution control to the additon() definition along with values of num1 and num2.
Then function definition executes the code written inside it and comes back to the function
In the concept of functions, the function call is known as "Calling Function" and the function
When we make a function call, the execution control jumps from calling function to called
function. After executing the called function, the execution control comes back to calling
function from called function. When the control jumps from calling function to called function
it may carry one or more data values called "Paramenters" and while coming back it may
carry a single value called "return value". That means the data values transferred from calling
function to called function are called as Parameters and the data value transferred from
Based on the data flow between the calling function and called function, the functions are
classified as follows...
Simply the execution control jumps from calling-function to called function and executes
called function, and finally comes back to the calling function. For example, consider the
following program...
PROGRAMMING FOR PROBLEM SOLVING UNIT 3
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
(parameters) but there is no data transfer from called function to calling-function (return
value). The execution control jumps from calling-function to called function along with the
parameters and executes called function, and finally comes back to the calling function. For
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
int num1, num2 ;
(parameters) but there is data transfer from called function to calling-function (return value).
The execution control jumps from calling-function to called function and executes called
function, and finally comes back to the calling function along with a return value. For example,
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
int result ;
return (num1+num2) ;
(parameters) and also from called function to calling-function (return value). The execution
PROGRAMMING FOR PROBLEM SOLVING UNIT 3
control jumps from calling-function to called function along with parameters and executes
called function, and finally comes back to the calling function along with a return value. For
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
int num1, num2, result ;
return (a+b) ;
Parameter Passing in C
When a function gets executed in the program, the execution control is transferred from
calling-function to called function and executes function definition, and finally comes back to
the calling function. When the execution control is transferred from calling-function to called-
function it may carry one or number of data values. These data values are called
as parameters.
Parameters are the data values that are passed from calling function to called
function.
PROGRAMMING FOR PROBLEM SOLVING UNIT 3
In C, there are two types of parameters and they are as follows...
• Actual Parameters
• Formal Parameters
The actual parameters are the parameters that are specified in calling function. The formal
parameters are the parameters that are declared at called function. When a function gets
executed, the copy of actual parameter values are copied into formal parameters.
In C Programming Language, there are two methods to pass parameters from calling function
• Call by Value
• Call by Reference
Call by Value
In call by value parameter passing method, the copy of actual parameter values are copied
to formal parameters and these formal parameters are used in called function. The changes
made on the formal parameters does not effect the values of actual parameters. That
means, after the execution control comes back to the calling function, the actual parameter
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
int num1, num2 ;
int temp ;
temp = a ;
a=b;
b = temp ;
Call by Reference
In Call by Reference parameter passing method, the memory location address of the actual
parameters is copied to formal parameters. This address is used to access the memory
locations of the actual parameters in called function. In this method of parameter passing,
That means in call by reference parameter passing method, the address of the actual
parameters is passed to the called function and is recieved by the formal parameters
(pointers). Whenever we use these formal parameters in called function, they directly access
the memory locations of actual parameters. So the changes made on the formal
parameters effects the values of actual parameters. For example consider the following
program...
Example Program
#include<stdio.h>
#include<conio.h>
void main(){
int num1, num2 ;
int temp ;
temp = *a ;
*a = *b ;
*b = temp ;
Recursive functions
Recursion is the process of calling a function itself repeatedly until a
particular condition is met. A function that calls itself directly or indirectly is
called a recursive function and such kind of function calls are called recursive
calls.
Recursive Functions
A function that calls a copy of itself is called Recursive Function. The
recursive functions contain a call to themselves somewhere in the function
body. Moreover, such functions can contain multiple recursive calls
Basic Structure of Recursive Functions
The basic syntax structure of the recursive functions is:
type function_name (args) {
// function statements
// base condition
// recursion case (recursive call)
}
To prevent infinite recursion, if...else statement (or similar approach) can be used
where one branch makes the recursive call, and other doesn't.
#include <stdio.h>
int sum(int n);
int main()
{
int number, result;
printf("Enter a positive integer: ");
scanf("%d", &number);
result = sum(number);
PROGRAMMING FOR PROBLEM SOLVING UNIT 3
printf("sum = %d", result);
return 0;
}
int sum(int n)
{
if (n != 0)
// sum() function calls itself
return n + sum(n-1);
else
return n;
}
/* Program to find factorial of a given number using recursion*/
#include <stdio.h>
int fact (int);
int main()
{
int n,f;
printf("Enter the number whose factorial you want to calculate?");
scanf("%d",&n);
f = fact(n);
printf("factorial = %d",f);
}
int fact(int n)
{
if (n==0)
{
return 0;
}
else if ( n == 1)
{
return 1;
}
else
PROGRAMMING FOR PROBLEM SOLVING UNIT 3
{
return n*fact(n-1);
}
}
Output
Enter the number whose factorial you want to calculate?5
factorial = 120
Let's see an example to find the nth term of the Fibonacci series.
#include<stdio.h>
int fibonacci(int);
void main ()
{
int n,f;
printf("Enter the value of n?");
scanf("%d",&n);
f = fibonacci(n);
printf("%d",f);
}
int fibonacci (int n)
{
if (n==0)
{
return 0;
}
else if (n == 1)
{
return 1;
}
else
{
return fibonacci(n-1)+fibonacci(n-2);
}
}
PROGRAMMING FOR PROBLEM SOLVING UNIT 3
Output
Enter the value of n?12
144
As we know that the array_name contains the address of the first element. Here, we
must notice that we need to pass only the name of the array in the function which is
intended to accept an array. The array defined as the formal parameter will
automatically refer to the array specified by the array name defined as an actual
parameter.
functionname(arrayname);//passing array
First way:
Second way:
Third way:
#include<stdio.h>
int minarray(int arr[],int size)
PROGRAMMING FOR PROBLEM SOLVING UNIT 3
{
int min=arr[0];
int i=0;
for(i=1;i<size;i++)
{
if(min>arr[i])
{
min=arr[i];
}
}//end of for
return min;
}//end of function
int main()
{
int i=0,min=0;
int numbers[]={4,5,7,3,8,9};//declaration of array
Output
minimum number is 3
Output
Storage classes
In C programming language, storage classes are used to define things like storage
location (whether RAM or REGISTER), scope, lifetime and the default value of a variable.
In C programming language, there are FOUR storage classes and they are as follows...
Property Description
Keyword auto
Life time Till the control remains within the block in which variable is defined
Example Program 1
#include<stdio.h>
#include<conio.h>
int main()
{
int i;
auto char c;
float f;
printf("i = %d\tc = %c\tf = %f",i,c,f);
return 0;
}
Example Program 2
#include<stdio.h>
#include<conio.h>
int main()
{
int a=10;
PROGRAMMING FOR PROBLEM SOLVING UNIT 3
{
int a=20;
printf("%d",a);
}
printf(" %d",a);
return 0;
}
Example Program 3
#include<stdio.h>
#include<conio.h>
int main()
{
{
int a=20;
printf("%d",a);
}
printf(" %d",a); //a is not visible here
return 0;
}
Property Description
Keyword extern
Life time As long as the program’s execution does not comes to end
Example Program 1
#include<stdio.h>
#include<conio.h>
int i; //By default it is extern variable
int main()
{
printf("%d",i);
return 0;
}
Example Program 2
#include<stdio.h>
#include<conio.h>
extern int i; //extern variable
int main()
{
printf("%d",i);
return 0;
}
end of the program. The static variable allows to initialize only once and can be modified any
number of times. Variable of static storage class has the following properties...
PROGRAMMING FOR PROBLEM SOLVING UNIT 3
Property Description
Keyword static
Default Zero
Value
Life time The value of the persists between different function calls (i.e.,
Initialization is done only once)
Example Program 1
#include<stdio.h>
#include<conio.h>
static int a;
int main()
{
printf("%d",a);
return 0;
}
Example Program 2
#include<stdio.h>
#include<conio.h>
allocated in CPU Registers. The memory of the register variable is allocated in CPU Register
but not in Computer memory (RAM). The register variables enable faster accessibility
compared to other storage class variables. As the number of registers inside the CPU is very
less we can use very less number of register variables. Variable of register storage class has
Property Description
Keyword register
Life time Till the control remains within the block in which variable is
defined
Example Program 1
#include<stdio.h>
#include<conio.h>
int main(){
register int a,b;
scanf("%d%d",&a,&b);
printf("%d %d",a,b);
}
PROGRAMMING FOR PROBLEM SOLVING UNIT 3
The following table provides detailed properties of all storage classes...