all about storage class


Storage class

Storage class of a variable refer to the lifetime, scope and visibility of a variable where,
  • Lifetime: time period for which the variable is active in memory.
  • Scope: Accessibility of the variable within the program.
  • Visibility: from which part  of the program the values of the variable is accessible.

Depending the storage class of a variable, a variable can be classified in four types.
  1. Automatic/Local
  2. External/global
  3. Static
  4. Register

  1. Automatic/local variable:


A variable declared within a function is called an automatic/local variable.
  • Lifetime: During the execution of the function containing it.
  • Scope & visibility: Scope and visibility are limited within the function only.
  • Priority of local variables is more than other variables.

2.Extern/Global variables:

A variable declared out side any function is an extern variable or global variable.
  • Lifetime: Available in memory throughout the execution of the entire program.
  • Scope & visibility: Visible thought the entire program after it’s definition.
  • A global variable being visible throughout all the functions, is modifiable by the entire program.


External variable

If a global variable is accessed by a function before defining it, a compile time error is reported
                          Void function()
                          {
                                                Printf(“\n x=%d”,++x);
/* here a compile time error will be reported saying ‘undeclared symbol x’  */
                                }
                                int x;
                                int main()
                                {
                                                printf(“\n x=%d”,++x);
                                                function();
                                                printf(“\n x=%d”,x);
return 0;
                                }

To resolve the problem, we use a key word ‘extern’.
                                Void function()
                                {
                                                extern int x;
/* Note that : this is not a variable declaration, but a procedure to inform the compiler about the presence of external variable */
                                                Printf(“\n x=%d”,++x);
                                }
                                extern x;
                                int main()
                                {
                                                printf(“\n x=%d”,++x);
                                                function();
                                                printf(“\n x=%d”,x);
return 0;
                           }
    these type of variables are called External variables ..

3.Static variable:

Static variable are initially set to zero when it is declared.
  • Lifetime: During the execution of the program containing it.


4.Register variables:

Register variables are stored in CPU registers. In a CPU the number of registers are limited. So, declaring register variables may produce error.



No comments

Powered by Blogger.