R4R
Right Place For Right Person TM
 
R4R CC TutorialsC Basic Tutorials1.6STORAGE 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:
(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.
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

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.

     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.

for 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

R4R CC TutorialsC Basic Tutorials1.6STORAGE CLASS IN C
Tolal:0 Click:
Show All Comments

Post Your Comments

Your Name:

Your Email ID :
Comments :
URL
  =* Enter SUM

Tutorials,Examples,Questions and Answer

R4R
R4R
R4R
R4R
R4R
R4R
R4R
R4R