C 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 the C programming language, the memory of variables is allocated either in computer memory (RAM) or CPU Registers. The allocation of memory depends on storage classes.
In C programming language, there are FOUR storage classes and they are as follows...
- auto storage class
- extern storage class
- static storage class
- register storage class
auto storage class
The default storage class of all local variables (variables declared inside block or function) is auto storage class. Variable of auto storage class has the following properties...
Property | Description |
---|---|
Keyword | auto |
Storage | Computer Memory (RAM) |
Default Value | Garbage Value |
Scope | Local to the block in which the variable is defined |
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;
{
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;
}
External storage class
The default storage class of all global varibles (variables declared outside function) is external storage class. Variable of external storage class has the following properties...
Property | Description |
---|---|
Keyword | extern |
Storage | Computer Memory (RAM) |
Default Value | Zero |
Scope | Global to the program (i.e., Throughout the program) |
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;
}
Static storage class
The static storage class is used to create variables that hold value beyond its scope until the 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...
Property | Description |
---|---|
Keyword | static |
Storage | Computer Memory (RAM) |
Default Value | Zero |
Scope | Local to the block in which the variable is defined |
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>
static int i=10;
int main(){
i=25; //Assignment statement
printf("%d",i);
return 0;
}
Register storage class
The register storage class is used to specify the memory of the variable that has to be 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 the following properties...
Property | Description |
---|---|
Keyword | register |
Storage | CPU Register |
Default Value | Garbage Value |
Scope | Local to the block in which the variable is defined |
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);
}
The following table provides detailed properties of all storage classes...
Storage Class | Keyword | Memory Location | Default Value | Scope | Life Time |
---|---|---|---|---|---|
Automatic | auto | Computer Memory (RAM) | Garbage Value | Local to the block in which the variable has defined | Till the control remains within the block in which variable is defined |
External | extern | Computer Memory (RAM) | Zero | Global to the program (i.e., Throughout the program) | As long as the program’s execution does not come to end |
Static | static | Computer Memory (RAM) | Zero | Local to the block in which the variable has defined | The value of the persists between different function calls (i.e., Initialization is done only once) |
Register | register | CPU Register | Garbage Value | Local to the block in which the variable has defined | Till the control remains within the block in which variable is defined |