Storage Class
In this chapter we will discuss different storage classes available in C. After this chapter you will be have understanding of different storage class available and the use of different storage classes.
Storage class in c defines the scope of variable or we can say the lifetime of a variable defined by you. Storage class tells us weather we are allowed to use any variable at some place or not.
In c we have four types of storage class
1) Auto
2) Register
3) static
4) External
1) Auto:- all the variable which is defined or declared without any storage class indicator is of by default Automatic storage class. These variable can be accessed only in the function or block in which its declared.
\
int a;
default value of auto variable is garbage.
example:-
{
int a=10;
printf(“allowed to use a=%d”,a); //it will not give any error as we are allowed to access the variable in the same block in which it is defined.
}
printf(“not allowed to use a=%d”,a); //it will give error as we are trying to access variable a outside the block in which it is defined.
fun()
{
int b=20;
b=10; //NO ERROR
}
b=30; //ERROR
2) Register:- Register variable is almost same as automatic variable and have the scope only in the block in which it is declared. It is declared with register identifier.
register int a;
but only difference is that register variable will be stored in registers instead of memory if register is available. But we have very limited register so when register is not available then it will store in memory just like automatic variable.
example:-
{
register int a=10;
printf(“allowed to use a=%d”,a); //it will not give any error as we are allowed to access the variable in the same block in which it is defined.
}
printf(“not allowed to use a=%d”,a); //it will give error as we are trying to access variable a outside the block in which it is defined.
fun()
{
register int b=20;
b=10; //NO ERROR
}
b=30; //ERROR
one very important thing about register variable is that we can not dereference register variable or we can not use address operator (&) with register variable as register variable stored in register and don’t have any address.
default value of register variable is garbage.
3)Static variable:-
static variable has file scope or we can say we can access the static veriable anywhere in file in which it is declared.
static variable is declared with static identifier
static int i;
example:-
{
register int a=10;
printf(“allowed to use a=%d”,a); //it will not give any error as we are allowed to access the variable anywhere in file in which it is defined.
}
printf(“allowed to use a=%d”,a); //it will not give any error as we are allowed to access the variable anywhere in file in which it is defined.
fun()
{
static int b=20;
b=10; //NO ERROR
}
b=30; // NO ERROR
value of static variable persist between calling of functions.
example:-
#include<stdio.h>
int main()
{
fun();
fun();
fun();
fun();
}
void fun()
{
static int count=0;
printf(“%d”,count);
count++;
}
output:- 0 1 2 3
when we call fun variable first time value of count will be zero and at the time of calling of fun() second time we will get value of count is 1 and in the same way in 3rd call of fun() we will get value of count 3 and so on.
default value of static variable is 0.
4) Global variable
As the name suggest global variable has global scope means we can access global variable through the file in which it is declared as well as in other files by using of extern keyword.
global variable is defined outside main function and has default value of zero.
example:
| File 1 |
| int a; |
| main() |
| { |
| printf(“value of a can be accessed here %d”,a); //it will not give any error |
| } |
| fun() |
| { |
| a=10; //NO ERROR |
| } |
|
variable a is declared in file 1 as a global variable if i want to access that variable in other file 2 then we can access by using external keyword
| File 2 |
| extern a; |
| main() |
| { |
| a=10; //allowed to access the variable a declared in file 1 |
| }