Storage Classes in C
A variable name identifies some physical location within the computer, where the string of bits representing the variable’s value, is stored.
There are basically two kinds of locations in a computer,_where such a value may be kept
(i) Memory (ii) CPU registers
It is the variable’s storage class that determines in which of the above two types of locations, the value should be stored.
We have four types of storage classes in C
- Auto 2. Register 3. Static 4. Extern
Auto Storage Class
Features of this class are given below
- Storage Location Memory
- Default Initial Value Garbage value
- Scope Local to the block in which the variable is defined.
- Life Till the control remains within the block in which variable is defined.
Auto is the default storage class for all local variables.
void main ( )
{
auto –int =3;
auto int j;
}
Register Storage Class
Register is used to define local variables that should be stored in a register instead of RAM. Register should only be used for variables that require quick access such as counters. Features of register storage class are given below
- Storage Location CPU register
- Default Initial Value Garbage value
- Scope Local to the block in which variable is defined.
- Life Till the control remains within the block in which the variable is
void main ( )
{
register int i ;
}
Static Storage Class
Static is the default storage class for global variables. Features of static storage class are given below
- Storage Location Memory
- Default Initial Value Zero
- Scope Local to the block in which the variable is defined. In case of global variable, the scope will be through out the program. •
- Life Value of variable persists between different function calls.
# i nclude <stdio.h> #i nclude <stdio.h>
void increment ( ) ; void increment ( )
void main ( ) void main ( )
{ {
increment ( ) ; increment ( ) ;
increment ( ) ; increment ( ) ;
} }
void increment ( ) void increment ( )
{ {
auto int =1; static int i = 1;
printf (“%d \n”, i ); printf (“%d \n”, 1);
i=i+ 1; i=i+1;
} }
Output 1 Output 1
2 2
Extern Storage Class
Extern is used of give a reference of a global variable that is variable to a the program files, When we use extern, the variable can’t be initialized as all, it does, is to point the variable name at a storage location that has been
previously defined.
- Storage Location Memory
- Default Initial Value Zero
- Scope Global
- Life As long as the program’s execution does not come to an end.
extern int y; * declaration*/
int y = 3 ; *definition*/
Features of extern storage class are given below
File 1 main.c
int count = 5;
main ( )
{
write extern ( );
}
File 2 write. c
void write_extern (void);
extern int count;
void write_extern (void)
{
printf (“count is %d \n”, count);
}