Storage Classes

Storage Classes:

                              Storage classes define the scope and life time of a variable declared.They are generally used during declaration of variables. 

Scope:

              Scope of a variable is defined as the part of the file where that variable can be accessed or modified.



#include <stdio.h>
int  main()
{
    int k=20;
    {
         int z=30;
         printf ("%d", k);   // this is allowed
    }
    printf ("%d",z);    // error(not allowed)
}    

As shown in the above example the variable z scope is defined to the inner block only, so it dies as soon as the block is over.(recall that a block starts with a { and ends with  a }).
When we try to access any variable, unexpected results are obtained, although no error is raised by compiler.

Life Time:

                    Life time can be defined as the time for which the variable is alive in the memory. 

Global Variable:

                    A global variable is a variable that is alive for the whole program and is generally declared out of all the blocks.  For example,


          
                   
#include <stdio.h>
int  g;
int main ()
{
     printf ("%d",g);
}

                  Here g is a global variable and can be accessed all over the program.

Local Variable:

                             Local variable is local to the block and cannot be accessed outside the block. So in the previous example if 'g' is declared inside main then it can only be accessed in the main block.
                             C language also provides with some storage classes which can be used to alter the storage of the variables. Let us see them:


1.Automatic storage class:

                            For this storage class the storage will be in memory and they get initialized with some garbage values when declared. The scope of these variables is with in a block and they are alive inside a block only. These variables can be declared by keeping a key word "auto" in front of the data type declaration. For Example,          auto int a;

                             By default all the variables that we declare belong to this storage class so int a is similar to auto int a.


2.Register storage class:  

                              For this storage class the storage will be in the CPU registers. CPU registers can be always accessed faster then the things stored in normal memory. So the variables that are frequently used can be stored in these registers. If the memory of register is not available the compiler allocates space in the main memory just like auto variables. These variables also get initialized with some garbage values. The scope and life time of this storage class is same as that of Automatic storage class i.e, block scope.

                               These can be declared by using the keyword "register" before the data type declaration. For example,      register int a;

3.Static storage class:

                              This storage class variable is stored in the normal memory, but they get initialized with zero after they are declared. The scope of this storage class is again limited to the block and life time persists between different function calls.

                              These variables can be declared using a keyword "static" before the data type declaration. For example,   static int a;

                               As the value of static value persists throughout the program, once declared, the value cannot be redeclared elsewhere in a program. An example will soon be added to this tutorial to understand how a static variable works.

4. Extern storage class:

                                 This storage class variable is also stored in memory, they also get initialized to zero when declared. The scope of this variables is global and can be used in some other program file. For example, let good.c is file that contain int a as extern storage class this a can be used in other program file. The life time of this storage classes is until the program's execution doesn't come to an end.

                                 These variables can be declared using a keyword "extern" before the data type declaration. For example,   extern int a;

                 The table below shows an quick overview of the storage classes discussed above:


Previous
Next Post »