A storage class specify scope and the lifetime of a variable with in a c program and also give information about where a variable gets memory space. We have four different storage classes in a c program.
1. Auto
2. Register
3. Static
4. Extern
1. Auto storage class
The auto storage class in the default storage
class for all local variable.
Auto – memory –
garbage
Int a;
auto int b;
Example of auto storage class
#include<stdio.h>
#include<conio.h>
void display(){
auto int i=1;
printf("%d\t",i++);
}
void main(){
int i;
clrscr();
for(i=1;i<=5;i++){
display();
}
getch();
}
Output
1 1 1 1 1
2. Register storage class
The memory for
the register variable is allocated in the CPU register for fast execution. The
‘register’ keyword is used to declare a register variable.
register – CPU register – garbage
register int b;
Example of register class
#include<stdio.h>
#include<conio.h>
void display(){
register int i=1;
printf("%d\t",i++);
}
void main(){
int i;
clrscr();
for(i=1;i<=5;i++){
display();
}
getch();
}
Output
1 1 1 1 1
3. Static storage class
A static variable
is local to a function with a lifetime as same as the program’s lifetime.
Static – memory – 0
Static int I;
Example of static storage class
#include<stdio.h>
#include<conio.h>
void display(){
static int i=1;
printf("%d\t",i++);
}
void main(){
int i;
clrscr();
for(i=1;i<=5;i++){
display();
}
getch();
}
Output
1 2 3 4 5
4. Extern storage class
It can be
accessed by any function of the program.The extern variable are alive
throughout the entire program.The extern variable is declare outside the
function.
extern – memory – 0
extern int I;
Example of extern storage class
#include<stdio.h>
#include<conio.h>
int i=5;
void main(){
extern int i;
clrscr();
printf("%d",i);
getch();
}
Output
5
0 Comments