Labels

Friday 28 August 2015

what are storage classes in c?

Storage Classes 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.

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:

1. Automatic storage class
2. Register storage class
3. Static storage class
4. External storage class
Let us examine these storage classes one by one.

1. 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.


2. 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 ) ;
}
Here, even though we have declared the storage class of i as register, we cannot say for sure that the value of i would be stored in a CPU register. Why? Because the number of CPU registers are limited, and they may be busy doing some other task. What happens in such an event... the variable works as if its storage class
is auto.

NOTE: Not every type of variable can be stored in a CPU register.

For example, if the microprocessor has 16-bit registers then they cannot hold a float value or a double value, which require 4 and 8 bytes respectively. However, if you use the register storage class for a float or a double variable you won’t get any error messages. All that would happen is the compiler would treat the variables to be of auto storage class.

3. 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.
main( )
{
     increment( ) ;
     increment( ) ;
     increment( ) ;
}
increment( )
{
     static int i = 1 ;
     printf ( "%d\n", i ) ;
     i = i + 1 ;
}
output: 1 2 3

the function increment( ) gets called from main( ) thrice. Each time it increments the value of i and prints it. static makes the variable exist even if the function returns. so next time increment called it won't create new memory for variable i and it will use the existing memory of i (created in first call).

 4. 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 that their scope is global, not local. External variables are declared outside all functions, yet are available to all functions that care to use them. Here is an example to illustrate this fact.

int i ;

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


Which to Use When???


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.











 




No comments:

Post a Comment