C Language: Storage classes in C programming language

storage class in c language

Storage class decides the part of storage to allocate memory for a variable, and also determines the scope of a variable. C language supports 4 types of storage classes and these are used to specify the scope of different variables defined within function blocks and programs.

The 4 types of storage class specifications are as follows.

  • auto
  • static
  • extern
  • register

Automatic variables

The variables without any storage class specification are considered automatic variables. Auto variables are defined inside a function. They are called automatic because their memory space is automatically allocated as the variable is declared.

These variables are given only temporary memory space and after the execution, all the automatic variables will get disposed of. It can not be accessed directly by other functions.

auto variable in c language

syntax: storage_ class_type        data_type var1,  var2. . ..varn;

Description: Memory space is automatically allocated as the variable is declared.

Example: auto int a, b;  (same as char a,b)

                 auto float x, y; (same as float x, y)

                 auto char sex; (same as char sex);

   int mount;
   auto int month;

The above example defines two variables with the same storage class, auto can only be used within functions, i.e., local variables.

Static Variable in C language

In C language static variable can also be an internal or external type, depending upon where it is declared. If declared outside the function body, it will be static or global. In case it is declared in the body or block it will be auto variable.

It is initialized only once, it is never reinitialized. The static variables are the variables for which the contents of the variables will be retained throughout the program.

With the use of this property of static variable, we can count number how many times a function is called. These are permanent within the function in which they are declared.

static variable in c language

Syntax: storage_class_type  data_type var1, var2, ..varn;

Description: Static variables are the variables for which the contents of the variables will be retained through out the program.

Example: static int a, b;

                  static float x,y;

                  static char sex;

Example Program

Program to show the use of the static variables.


p=1   p=2  p=3  p=4  p=5

Explanation: The print() function is called from the main program the variable P is a static variable. It will be incremented and printed in each call.

External variables in c language

The external variables are declared out of the main() function. The availability of these variables is throughout the program and that is both in the main program and inside the user-defined functions.

The compiler does not allocate memory for these variables. It is already allocated for it another module where it is declared as a global variable.

extern variable in c language

syntax: storage_class_type  data_type var1, var2, … varn;

Description: The external variables are available in the main program and inside the user-defined function.

Example: extern int a,b;

                 extern float x,y;

                 extern char sex;

In C language the external declaration of the variables makes the variables to be available even for the external functions that are called during program execution.

Example Program

Program to show the working of auto and global variables with the same name.



In calla() D = 142

In callb() D = 150

In main() D = 150

Explanation: Here the variable, d is declared outside the main() function and d is initialized as 150. Every function can access the variable d.

So no re-declaration or local variable is created. The value of d is printed by all the functions in the program.

Register variables in C language

C language Registers are special storage areas within a computer’s Central Processing Unit.

The actual arithmetic and logical operations that comprise a program the program is carried out within these registers. The register access is faster than the memory access. CPU registers are limited numbers in numbers. Hence we cannot declare more variables with register variables.

We cannot use the register keyword for all types of variables.

syntax: storage_class_type  data_type var1, var2,..varn;

Description: Register tells the compiler that the variable list followed by it is kept on the CPU registers.

Example: register int a,b;

Example Program

Program to demonstrate usage of register variable.

void main()
    register int i = 0; /* Local deginition */
    /* statement */
    for( i = 0;i < = 3; i++)
       printf("value of i is : %d\n", i);
    } /* for */
}   /* main */


value of i is : 0
value of i is : 1
value of i is : 2
value of i is : 3

Explanation: The variable i declared as register type integer variable and initialized as zero. The value of i is incremented at each time through for loop and prints the i value as an output.

Storage class specification of variables provides information about the location and visibility of the variables.

The following table illustrates the different features of storage classes.

storage classes In C language