Best Industrial Training in C,C++,PHP,Dot Net,Java in Jalandhar

Monday, 20 January 2014

Storage Classes in C Programming Language

A storage class is an attribute that tells us where the variable would be stored, what will be the initial value of the variable if no value is assigned to that variable, life time of the variable and scope of the variable.

Syntax: storage_specifier data_type variable _name
There are four storage classes in C:
1) Automatic storage class
2) Register storage class
3) Static storage class
4) External storage class
There are 4 storage class specifiers available in C language. They are,

1. auto
2. extern
3. static
4. register

S.No.
Storage Specifier
Storage place
Initial / default value
Scope
Life
1
auto
CPU Memory
Garbage value
local
Within the function only.
2
extern
CPU memory
Zero
Global
Till the end of the main program. Variable definition might be anywhere in the C program
3
static
CPU memory
Zero
local
Retains the value of the variable between different function calls.
4
register
Register memory
Garbage value
local
Within the function

Note:

· For faster access of a variable, it is better to go for register specifiers rather than auto specifiers.

· Because, register variables are stored in register memory whereas auto variables are stored in main CPU memory.

· Only few variables can be stored in register memory. So, we can use variables as register that are used very often in a C program.




Automatic storage class:
The keyword used for Automatic storage class is 'auto'.
The variable declared as auto is stored in the memory.
Default value of that variable is garbage value.
Scope of that variable is local to the block in which the variable is defined.
Variable is alive till the control remains within the block in which the variable is defined.
Example:
#include<stdio.h>
#include<conio.h>
void main(){
auto int a;
printf(“%d”,a)
}
Output:
1285
As seen above, the output is garbage value.

Register storage class:
The keyword used for Register storage class is 'register'.
The variable declared as register is stored in the CPU register.
Default value of that variable is garbage value.
Scope of that variable is local to the block in which the variable is defined.
Variable is alive till the control remains within the block in which the variable is defined.
Main difference between auto and register is that variable declared as auto is stored in memory whereas variable declared as register is stored in CPU register. Since the variable is stored in CPU register, it takes very less time to access that variable. Hence it becomes very time efficient.
It is not necessary that variable declared as register would be stored in CPU registers. The number of CPU registers is limited. If the CPU register is busy doing some other task then variable might act as automatic variable.

Example:
#include<stdio.h>
#include<conio.h>
Void main(){
register int a;
printf(“%d”,a)
}
Output:
4587
As seen above, the output is garbage value.

Static storage class:
The keyword used for Static storage class is 'static'.
The variable declared as static is stored in the memory.
Default value of that variable is zero.
Scope of that variable is local to the block in which the variable is defined.
Life of variable persists between different function calls.

External storage class:
The keyword used for External storage class is 'extern'.
The variable declared as static is stored in the memory.
Default value of that variable is zero.
Scope of that variable is global.
Variable is alive as long as the program’s execution doesn’t come to an end.

External variable can be declared outside all the functions or inside function using 'extern' keyword.
Example:
#include<stdio.h>
#include<conio.h>
int a;
Void main(){
extern int b;
printf(“%d %d”,a,b)
}
int b=10;
Output:
0 10










Example program for auto variable in C:

#include<stdio.h>
void increment(void);
int main()
{
increment();
increment();
increment();
increment();
return 0;
}

void increment(void)
{
auto int i = 0 ;
printf ( "%d ", i ) ;
i++;
}

Output:
0 0 0 0

//C static example

#include<stdio.h>
void increment(void);
int main()
{
increment();
increment();
increment();
increment();
return 0;
}
void increment(void)
{
static int i = 0 ;
printf ( "%d ", i ) ;
i++;
}

Output:
0 1 2 3

Example program for extern variable in C:

The scope of this extern variable is throughout the main program. It is equivalent to global variable. Definition for extern variable might be anywhere in the C program.

#include<stdio.h>
int x = 10 ;
int main( )
{
extern int y ;
printf ( "The value of x is %d \n", x ) ;
printf ( "The value of y is %d",y ) ;
return 0;
}
int y = 50 ;

Output:
The value of x is 10
The value of y is 50

Example program for register variable in C:

Register variables are also local variables, but stored in register memory. Whereas, auto variables are stored in main CPU memory.
Register variables will be accessed very faster than the normal variables since they are stored in register memory rather than main memory.
But, only limited variables can be used as register since register size is very low. (16 bits, 32 bits or 64 bits)

#include <stdio.h>
int main()
{
register int i;
int arr[5]; // declaring array
arr[0] = 10; // Initializing array
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
for (i=0;i<5;i++)
{
// Accessing each variable
printf("value of arr[%d] is %d \n", i, arr[i]);
}
return 0;
}

Output:

value of arr[0] is 10

value of arr[1] is 20

value of arr[2] is 30

value of arr[3] is 40

value of arr[4] is 50


No comments:

Post a Comment