Storage Classes in C
To fully define a variable one needs to mention not only its ‘type’ but also its ‘storage class’. In other
words, not only do all variables have a data type, they also have a ‘storage class’.
Storage classes have defaults. If we don’t specify the 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.
There are basically two kinds of locations in a computer where a value may be kept— Memory and CPU
registers. It is the variable’s storage class that determines in which of these two locations the value is
stored.
A variable’s storage class tells us:
(a) Where the variable would be stored.
(b) What will be the initial value of the variable, if initial value is not specifically assigned.(i.e. the default
initial value).
(c) What is the scope of the variable; i.e. in which functions the value of the variable would be available.
(d) What is the life of the variable; 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
Automatic Storage Class
The features of a variable defined to have an automatic storage class are as under:
Storage − Memory.
Default initial value − An unpredictable value, which is often called a garbage value.
Scope − Local to the block in which the variable is defined.
Life − Till the control remains within the block in which the variable is defined.
Register Storage Class
The features of a variable defined to be of register storage class are as under:
Storage - CPU registers.
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 the variable is defined.
A value stored in a CPU register can always be accessed faster than the one that 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. We can name their storage class
as register.
main( )
register int i ;
for ( i = 1 ; i <= 10 ; i++ )
printf ( "\n%d", i ) ;
Static Storage Class
The features of a variable defined to have a static storage class are as under:
Storage − Memory.
Default initial value − Zero.
Scope − Local to the block in which the variable is defined.
Life − Value of the variable persists between different function calls.
The difference between auto variables and static variables is that static variables don’t disappear when
the function is no longer active. Their values persist. If the control comes back to the same function
again the static variables have the same values they had last time around.
External Storage Class
The features of a variable whose storage class has been defined as external are as follows:
Storage − Memory.
Default initial value − Zero.
Scope − Global.
Life − As long as the program’s execution doesn’t come to an end.
- Use static storage class only if you want the value of a variable to persist between different function
calls.
− Use register storage class for only those variables that are being used very often in a program. Reason
is, there are very few CPU registers at our disposal and many of them might be busy doing something
else. Make careful utilization of the scarce resources. A typical application of register storage class is
loop counters, which get used a number of times in a program.
− Use extern storage class for only those variables that are being used by almost all the functions in the
program. This would avoid unnecessary passing of these variables as arguments when making a function
call. Declaring all the variables as extern would amount to a lot of wastage of memory space because
these variables would remain active throughout the life of the program.
− If you don’t have any of the express needs mentioned above, then use the auto storage class. In fact
most of the times we end up using the auto variables, because often it so happens that once we have
used the variables in a function we don’t mind loosing them.