Storage classes in c

From the 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.

  • Where the variable would be stored.
  • What will be the initial value of the variable, if the initial value is not specifically assigned.
  • What is the scope of the variable; i.e. in which functions the value of the variable would be available.
  • What is the life of the variable; i.e. how long would the variable exist.

Types of Storage classes

There are four storage classes in C:

  1. Automatic storage class
  2. Register storage class
  3. Static storage class
  4. External storage class

Automatic Storage Class:

The features of a variable defined to have an automatic storage class are as under:

Automatic Storage Class

Register Storage Class:

The features of a variable defined to be of register storage class are as under:

Register Storage Class

Static Storage Class:

The features of a variable defined to have a static storage class are as under:

Static Storage Class

External Storage Class:

The features of a variable whose storage class has been defined as external are as follows:

External Storage Class

Uses of Storage Classes

There are the following areas or ways to uses the storage classes.

  • Use static storage class only if you want the value of a variable to persist between different function calls.
  • Use register storage class for only those variables that are being used very often in a program. The reason is, there are very few CPU registers at our disposal and many of them might be busy doing something else. Make careful utilization of the scarce resources. A typical application of the register storage class is loop counters, which get used a number of times in a program.
  • Use the extern storage class for only those variables that are being used by almost all the functions in the program. This would avoid the unnecessary passing of these variables as arguments when making a function call. Declaring all the variables as an extern would amount to a lot of wastage of memory space because these variables would remain active throughout the life of the program.
  • If you don’t have any of the express needs mentioned above, then use the auto storage class. In fact most of the time we end up using the auto variables because often it so happens that once we have used the variables in a function we don’t mind losing them.