Storage Class

 

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

 

Variable Type Automatic Register Static Extern
Location Memory CPU register Memory Memory
Initial value Garbage Garbage Zero Zero
Scope Local to the block (function) Local to the block (function) Local to the block (function) Global (accessible to all the functions)
Life Created anew on each entry to the block and cease to exist when the block is exited Created anew on each entry to the block and cease to exist when the block is exited The value persists after the block is exited and again the block is reentered The life of the program
Definition and declaration Auto char c; OR char c; Register int k; Static float sum; Float sum; at the start of the program and ahead of any function. If the definition is following the function then extern declaration is required in the function as extern float sum;

 

Automatic

The keyword used for Automatic storage class is ‘auto’. he 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 id 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 id 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

 

 

 

 

 

 

 

 

Registration


A password will be e-mailed to you.

Feedback Form

Name (required)

Email (required)

Feedback