Storage Classes :
In C language, each variable has a storage class which is used to define scope and life time of a variable.
Storage: Any variable declared in a program can be stored either in memory or registers. Registers are small amount of storage in CPU. The data stored in registers has fast access compared to data stored in memory.
Storage class of a variable gives information about the location of the variable in which it is stored, initial value of the variable, if storage class is not specified; scope of the variable; life of the variable.
There are four storage classes in C programming.
1 : Automatic Storage class.
2 : Register Storage class.
3 : Static Storage class.
4 : External Storage class.
1: Automatic Storage class : To define a variable as automatic storage class, the keyword „auto‟ is used. By defining a variable as automatic storage class, it is stored in the memory. The default value of the variable will be garbage value. Scope of the variable is within the block where it is defined and the life of the variable is until the control remains within the block.
Syntax : auto data_type variable_name; auto int a,b;
2: Register Storage class : To define a variable as register storage class, the keyword „register‟ is used. If CPU cannot store the variables in CPU registers, then the variables are assumed as auto and stored in the memory. When a variable is declared as register, it is stored in the CPU registers. The default value of the variable will be garbage value. Scope of the variable within the block where it is defined and the life of the variables is until the control remains within the block.
3 : Static Storage class : When a variable is declared as static, it is stored in the memory. The default value of the variable will be zero. Scope of the variable is within the block where it is defined and the life of the variable persists between different function calls. To define a variable as static storage class, the keyword „static‟ is used. A static variable can be initialized only once, it cannot be reinitialized.
4 : External Storage class : When a variable is declared as extern, it is stored in the memory. The default value is initialized to zero. The scope of the variable is global and the life of the variable is until the program execution comes to an end. To define a variable as external storage class, the keyword „extern‟ is used. An extern variable is also called as a global variable. Global variables remain available throughout the entire program. One important thing to remember about global variable is that their values can be changed by any function in the program.
extern keyword :
The extern keyword is used before a variable to inform the compiler that this variable is declared somewhere else. The extern declaration does not allocate storage for variables .