Storage classes - C

  • Automatic storage class.
  • Static storage class.
  • External storage class.
  • Register storage class.

Every variable has a storage class, which may be specified explicitly or it may be implied by the place at which the variable is defined in the source code.

Storage class tells us:

  1. Where the variable is stored? In memory? or In register?
  2. What is the scope of the variable i.e. how far the variable can be accessed, what function can use it and so on.
  3. The life of the variable. i.e. upto what extent a variable can retain its value.

The above points will be clear when we consider each storage class in detail.

Automatic storage class
They are called automatic or the local variables. Local variables are stored in memory. If a local variable is not initialized it will contain garbage value. Automatic variables are always declared within a function. The scope of an automatic variable is confined to the function in which they are declared or to the block of statement in which they are declared. The value of the variable is lost after control goes out of the block in which it is defined and is re-initialized when the block or function is reentered.

/* program show the automatic storage class * /
/ * the output helps us understand what is the scope of variable
k when defined in different places with automatic storage class
In function main */

The scope of variable ‘k = 1’, defined in main() outside for loop is the entire function main().

Inside for loop
k = 2
Outside for loop
k = 1

External storage class
External variable are defined outside of any function. The scope of external variable extends from the point at which it is defined to the end of the program. Scope of external variable is said to be global as against automatic variables. Even if external variable is not defined, its initial value is taken to be ‘0’ by default. External variable is recognized by location of variable definition within the program.

/* program to showexternal variable declaration and scope */

In main b=0
in corn before incrementing a
In main
In play

Here ‘a’ is defined to be an external variable. It is defined outside main(), hence its scope is the entire program. Definition of external variable allocates space for the variable. In order to use variable ‘a’ in function corn(), we have to declare it in corn() as ‘extern int a’. Storage specifiers is to be mentioned in extern .Variable declaration at this place in corn is optional because it is defined outside and above corn() in the same source file. But it is a must to declare external variables.

When we are using them before defining them (as for variable b which is defined before corn) and secondly when we want to use an external variable defined in another file. Any change made to an external variable by any function is recognized within the entire scope of the variable. Using external variable also reduces the number of argument to be passed to the calling function. As in our example, if we want to use ‘a’ in function corn(), we need not pass it. It is available to corn() since ‘a’ is an external variable.

The variable ‘b’ defined in function play() is local to the function. It takes precedence over the external variable defined outside function corn(). i.e. when control comes to function play, b = 10; is effective.

Static storage class
Static variable are stored in memory. If not initialized, its default value is zero. Static variables are always declared within a function like automatic variable but with a storage class specifier ‘static’.The scope of a static variable is confined to the function in which it is declared or to the block of statements in which they are declared.

The basic difference between automatic and static variable is that static variable unlike automatic variable retains its value even if control goes out of the function in which it is defined. When function is re-entered, the variable is not re-initialized but uses last value assigned to it. The progrm show the automatic storage class used for automatic variables with slight modifications will help you clear the concept of static variable.

/* program to demonstrate use of static storage class
variable i retains its value during second call to function exmp */

The output of above program will be
Inside exmp
Inside exmp

here when main calls exmp() first the value of i = 10 is printed and i is incremented. When main calls exmp() again, this i = 11 is retained (variable is not initialized again).

i = 11 is printed and i is incremented. In the program to show the automatic storage where variable i, in function exmp is automatic variable, first call prints i=10 and the incremented value is lost when function returns. When main calls exmp again i is reinitialized to 10 and again printed as i = 10

Register storage class
The register storage class specifier indicates to the compiler that the value of the object should reside in a machine register. The compiler is not required to honor this request. Because of the limited size and number of registers available on most systems, few variables can actually be put in registers. If the compiler does not allocate a machine register for a register object, the object is treated as having the storage class specifier auto. A register storage class specifier indicates that the object, such as a loop control variable, is heavily used and that the programmer hopes to enhance performance by minimizing access time.

Normally when operation are carried out, it is done by transferring information from the computers memory to the registers, performing operation and then transferring the results back to the computers memory. When a variable is required to be used many times, considerable execution time is required in transferring from memory to register and back. In such cases register storage class is used. When the variable is stored in register itself, rather than memory, value can be accessed faster. The definition is as follows

register int i;

There is no default value assigned to register variables and will contain garbage value if not initialized. There is lot of similarity between automatic variables and register variables. The scope of register variable is confined to the block or function in which it is defined. The register looses its value when control is out of the block in which it is defined and is re-initialized on re-entering the block.


Here storage class of i is register. When we define storage class of a variable to be register, it does not necessarily mean that a register is for sure assigned, because the numbers of CPU (central processing unit) registers are limited. When register is not assigned, the variable is assigned automatic storage class.

Register storage class cannot be used for all types of variables. They are mainly used for integer type of variables. This is because of the size of register in C.P.U., which are generally 16 bits and cannot store float, double and long values which require more space.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

C Topics