C Programing laungage

Storage class in C
Previous Home Next

Storage class in C

From C compilerís point of view, 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ó Memory and CPU registers. It is the variableís storage class that determines in which of these two locations the value is stored.

Moreover, a variableís storage class tells us:

  1. Where the variable would be stored.
  2. What will be the initial value of the variable, if 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 variable; i.e. how long would the variable exist.

There are four storage classes in C:

  1. Automatic storage class
  2. Register storage class
  3. Static storage class
  4. 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.

Following program shows how an automatic storage class variable is declared, and the fact that if the variable is not initialized it contains a garbage value:

main( )
{
auto int i, j ;
printf ( "\n%d %d", i, j ) ;
}


The output of the above program could be...
1211 221

where, 1211 and 221 are garbage values of i and j. When you run this program you may get different values, since garbage values are unpredictable. So always make it a point that you initialize the automatic variables properly, otherwise you are likely to get unexpected results. Note that the keyword for this storage class is auto, and not automatic.

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.

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.

External storage class

Storage - Memory.
Default initial value - Zero.
Scope - Global.
Life - As long as the programís execution doesnít come to an end.

 

External variables differ from those we have already discussed in b that their scope is global, not local. External variables are declared outside all functions.

Example

main( )
{
printf ( "\ni = %d", i ) ;
increment( ) ;
increment( ) ;
decrement( ) ;
decrement( ) ;
}
increment( )
{
i = i + 1 ;
printf ( "\non incrementing i = %d", i ) ;
}
decrement( )
{
i = i - 1 ;
printf ( "\non decrementing i = %d", i ) ;
}
The output would be:
i = 0
on incrementing i = 1
on incrementing i = 2
on decrementing i = 1
on decrementing i = 0

As is obvious from the above output, the value of i is available to the functions increment( ) and decrement( ) since i has been declared outside all functions.

Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: