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.
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
(b) Static
                           (c) Register
(d) External




Automatic variables
ü  These are defined inside a function.  A variable declared inside a function without storage class name, by default is an auto variable.
 The features of automatic variables are:-

            (i)  Storage                  : memory
            (ii) initial value            : garbage  (or)  unpredictable
            (iii) scope                     : within the function
            (iv) Life time               : till the control remains in the function.

ü  These variables are created when the function is called and destroyed automatically when the function is exited. Automatic variables are local to the function in which they are declared.  These values cannot be accessed by any other function.  The keyword used is ‘auto’.
Example:         main( )
{
     auto int  i, j ;
     printf ( "\n%d %d", i, j ) ;
}
Static variables
Ø  Static variables may be of Local (or) global depending upon where it is declared.  If it is declared outside the function, it is static global otherwise if it declared inside a function block, it is static local.

Ø  A static variable is initialized only once and can never be re-initialized.  The value of static variable persists at each call and last change made in the variable remains throughout the program execution.

Ø  Static variables are stored in the computer global memory area .

Ø  The keyword used to declare these variable is “static”.
The features of a static variable are:-

      (i) Storage                   : memory
      (ii) initial value            : zero
      (iii) scope                     : Local to the block in which variable is defined.
      (iv) Life time               : persists till the end of program execution.




Example :-                 void main( )
{            incr( );
incr( );
incr( );
}
void incr( )
{
static int x;
x=x+1;
printf(“%d”, x);
}
Register Variables
Ø  Instead of string in memory, Register variables can also be stored in register of cpu.

Ø    The advantage of storing in registers is register access is faster than memory access, so, generally frequently accessed variables are kept in registers for foster execution of the program.
the features of register variables are:-
                (i) Storage                   : Registers
            (ii) initial value            : Garbage
            (iii) scope                     : Local
            (iv) Life time               : un till the control remains in that function block.
Example:-        
void main( )
            {
                  register int i;
                  for (i=1; i<=5; i++)
                  printf (“ %d/t”, i);
            }
External variables

Ø  External variables are also known as global variables.  These variables are declared outside the function and the values of these variables are available to all the functions of the program.

Ø  Unlike Local Variables, Global Variables can be accessed by any function in the program.If same name is given to both the global and local variables priority is given to the local variable.  The keyword “ extern” is used to declare these variables.

            The features of external variables are:

   (i) Storage                   : memory
   (ii) initial value            : zero
   (iii) scope                     : Global
   (iv) Life time               : till the program comes to an end.

No comments:

Post a Comment