STORAGE CLASSES in 'C' language
To fully define a variable one needs to mention not only its type, but also its 'storage class' . Every variable not only has a data type but also have a storage class. If don't specify any storage class of a variable in its declaration, the compiler will assume a storage class depending on the context in which the variable is used. Thus, variables have certain default storage classes. A variable name identifies some physical location within the computer where the string of bits representing the variable value is stored. There are basically two kinds of locations in a computer where such a value may be kept memory and CPU registers. It is the variable's storage class, which determines in which of these two locations the value is stored.
A variable storage class tells us
1) Where the variable would be stored.
2) What will be the initial value of the variable, if the initial value is not specifically assigned (i.e, the default initial value).
3) What is the scope of the variable, i.e., in which functions the value of the variable would be available.
4) What is the life of the variables; i.e., how long would the variable exist.
There are four storage classes in C.
a) Automatic storage class
b) Register storage class
c) Static storage class
d) External storage class
a) Automatic storage class:- These variables comes into existence whenever and wherever the variable is declared. These variables are also called as local variables, because these are available local to a function. The storage is in the memory and it has a default value, which is a garbage value. It is local to the block in which it has been declared and it life is till the control remains within the block in which the variable is defined. The key word used is 'auto'.
b) Register storage class:- The storage of this type of variables is in the CPU registers. It has a garbage value initially. The scope of the variable is it is local to the block in which the variable is defined. Its life is till the control remains in the block in which it is defined. A value stored in a CPU register cal always be accessed faster than the one which is stored in memory. Therefore, if a variable is used at many places in a program it is better to declare its storage class as register. A good example of frequently used variables is loop counters. The key word used is 'register'.
c) Static storage class:- The storage is in the memory and default initial value is zero. It is local to the block in which it has been defined. The value of the variable persists between different function calls. The value will not disappear once the function in which it has been declared becomes inactive. It is unavailable only when you come out the program. The key word used is 'static'.
static int a=5;
The output of the program is not 7,7,7 but it is, 7,9,11.
d) Extern storage class:- The variable has a storage in the memory. Default initial value is zero. The scope of the variable is global. It is present as long as the program execution comes to an end. The keyword used is 'extern'.
Please send comments to email@example.com