400+ Editable Festival Posters + Free Bonuses

PART 1 : Lesson 8 : Storage Classes in C Language


Automatic Storage Class
Register Storage Class 
Static Storage Class
External Storage Class


From C complier’s point of view, a variable name identifier 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
§  CPU registers.
It is the variable’s storage class which determines in which of these two locations the value is stored.
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 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 function the value of the variables would be available?
4.   What is the life of the variable, i.e. how long would the variables exist?



            There are four types of storage classes in C Language:
1)      Automatic storage class
2)      Register storage class
3)      Static storage class
4)      External storage class

1)      Automatic Storage Class:-

The features of a variable defined to have an automatic storage class are as under: -

1.      Storage – Memory
2.      Default initial value – an unpredictable value, which is often called a garbage value.
3.      Scope – local to the block in which the variable is defined.
4.      Life – till the control remains within the block in which the variable is defined.
Example:-
void main( )
{
            auto int i=1;
            {
                        auto int i=2;
            {
                                    auto int i=3;
                                    printf ("%d", i);
            }
            printf ("\n %d", i);
}
printf ("\n %d", i);
}


Output:-
                                                            3
                                                            2
                                                            1

2)      Register Storage Class:-

The features of a variable defined to have a register storage class are as under:

1.      Storage – CPU Register.
2.      Default initial value –Garbage value.
3.      Scope – local to the block in which the variable is defined.
4.      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 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. It is 16-bit registers and therefore cannot hold a float value or a double value.

3)      Static Storage Class:-

The features of a variable defined to have a static storage class are as under:

1.      Storage – Memory           
2.      Default initial value – Zero.
3.      Scope – local to the block in which the variable is defined.
4.      Life – value of variable persists between different function calls.

Example:- (Difference between the Automatic and Static Storage Classes)
                       
                                    Automatic Storage Class                            Static Storage Class

                                    void increment( );                                            void increment( );
                                    void main ( )                                                    void main ( )
                                    {                                                                      {
                                                increment ( );                                                   increment ( );
                                                increment ( );                                                   increment ( );
                                                increment ( );                                                   increment ( );
}                                                                      }
                       
void increment ( )                                                        void increment ( )
{                                                                      {
                                                auto int i=1;                                                     static int i=1;
                                                printf ("%d \n", i);                                            printf ("%d \n", i);
                                                i=  i+1;                                                             i=  i+1;
}                                                                      }

Output:-                                                          Output:-
                                                1                                                                      1
                                                1                                                                      2
                                                1                                                                      3

4)      External Storage Class:-

The features of a variable whose storage class has been defined as external storage class are as under:

1.      Storage – Memory           
2.      Default initial value – Zero.
3.      Scope – Global.
4.      Life – As long as the program’s execution does not come to an end.

External variables are declared outside all functions yet are available to all functions that cause to use them.

Example 1:-
void increment ( );
void decrement ( );
int i;

void main ( )
{
            printf ("\ni= %d", i);
            increment ( );
            increment ( );
            decrement ( );
            decrement ( );
}
void increment ( )
{
            i=i+1;  
            printf ("\nOn increment i=%d", i);
}
void decrement ( )
{
            i=i-1;
            printf ("\nOn decrement i=%d", i);
}


Output:-
i=0
On increment i=1
On increment i=2
On decrement i=1
On decrement i=0

      Note: - Here i has been declared outside all functions.


Example 2:-
void display ( );
int x=10;

void main( )
{
            int x=20;
            printf ("%d", x);
            display ( );
}
void display ( )
{
            printf ("\n%d", x);
}
           
Output:-         20
10


There are few reasons of using these storage classes. They are as follow:

Ø  Economically utilizes the memory space consumed by the variables.
Ø  Improve the speed of execution of the program.

            Rules are under:-

Ø   Use static storage class only if you want the value of a variable to persist between function calls. A typical application of this storage class is recursive functions.

Ø     In register storage class for only those variables which are being used vary often in a program. Reason is, there are very few CPU register at our disposal and many of them might be busy doing something else. A typical application of register storage class is loop counters, which get used a number of times in a program.

Ø    Use external/extern storage class for only those variables which 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/external would amount to a lot of wastage of memory space because these variables 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 it often happens that once we have need the variables in a function we don’t mind of losing them.






0 comments:

Post a Comment

Awesome Gadgets for Students and IT People