C Storage Class

Every variable has a storage class which defines the characteristics of the variable. It tells the compiler where to allocate memory for a variable, it also determines the scope of the variable, visibility and lifetime of a variable. There are four types of storage class :

  • Automatic
  • External
  • Static
  • Register

Automatic(auto) Storage class:

A variable defined within a function or block with auto specifier belongs to automatic storage class. Auto variables can only be accessed within the block/function they have been declared and not outside them (which defines their scope).

External(extern) Storage Class:

Extern stands for external storage class. The principal use of extern is to specify that a variable is declared with external linkage elsewhere in the program. In general, it is used to declare a variable to be used in a module that is not the one in which the corresponding variable is defined.

Static Storage Class:

Another class is called static. The name static is given to variables which can hold their values between calls of a function: they are allocated once and once only and their values are preserved between any number of function calls. Space is allocated for static variables in the program code itself and it is never disposed of unless the whole program is.
NOTE: Every global variable, defined outside functions has the type static automatically. The opposite of static is auto.

Register Storage class:

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 scarce resources. A typical application of register storage class is loop counters, which get used a number of times in a program.