Storage classes in c language


  • The storage class determines the storage duration, scope and linkage. The variable‚Äôs storage duration is the period during which the variable exists in the memory.
  • Some kinds of variables are repeatedly created in the memory, and some exist throughout the program execution.
  • The variables declared in C programs are different from the ones in other languages. We can use the same variable names in the C program in separate blocks.
  • When we declare a variable, it is available only to the specific part or block of the program. The remaining block or other function cannot access the variable.
  • Variables declared within the function are called as internal variables and those declared outside the function are called as external or global variables.
    Storage Classes Storage Place Default Value Scope Lifetime
    auto RAM Garbage Value Local Within function
    extern RAM Zero Global Till the end of main program, May be declared anywhere in the program
    static RAM Zero Local Till the end of main program, Retains value between multiple functions call
    register Register Garbage Value Local Within function

Any variable declared in C can have any one of the four storage classes:

  • Automatic variables
  • External variables
  • Static variables
  • Register variables

Automatic variables

  • auto variables are defined inside a function. A variable declared inside the function without storage class name is, by default, an auto variable.
  • A keyword auto is used for declaration of automatic variables. By default, every variable pertains to auto storage class.
  • These variables are available only to the current block or program.
  • Once the executions of the function take place and the return turns off the function, the contents and existence of the auto variables or local variables vanish.
  • Whenever a function is executed, all auto variables are allocated memory, and deallocated when the execution of function ends.

External variables

  • The external storage class indicates that the variable has been defined at other place other than the place where it is declared.
  • It can be declared in another source file. The variable declaration generally appears before the main() and use of keyword extern is optional.
  • Initialization cannot be done because its value is defined in another source file. Memory is not allotted in the source program, and it is allotted where it is defined.
  • The extern variable is visible to all the programs. It is used if two or more files are sharing same variable or function.
  • The compiler does not allocate memory for these variables. It is already allocated for it in another module where it is declared as global variable.

Static variables

  • The static variable may be an internal or external type, depending upon where it is declared.
  • If declared outside the function body, it will be static global. In case declared within the body or block, it will be auto variable.
  • When a variable is declared as static, its garbage value is removed and initialized to NULL value. The contents stored in these variables remain constant throughout the program execution.
  • The static variable is initialized only once and exists till the end of the program. It retains its value between multiple functions call.The static variable has the default value 0 which is provided by compiler.
  • A static variable is initialized only once; it is never reinitialized.

Register variables

  • The extern variable is visible to all the programs. It is used if two or more files are sharing same variable or function.
  • We can also keep some variables in the CPU registers instead of memory. The keyword register tells the compiler that the variable list followed by it is kept in the CPU registers, since register access is much faster than the memory access.
  • If the CPU fails to keep the variables in CPU registers, in that case the variables are assumed as auto and stored in memory.
  • CPU registers are limited in numbers. Hence, we cannot declare more variables with register variables.
  • The data type float and double need space more than 16 bits. If we define variables of these data type with register class, no errors will be shown. The compiler treats them as variables of auto class.