Storage Classes in C | C - Storage Classes

Storage classes are used to describe the features of a variable. Every variables in C has two attributes, type and storage class. Type refers to the data type of a variable, and storage class determines the scope or visibility, life time, memory location and initial value of a variable. In our programs, we didn’t mention storage class of the variables used. This is because of the fact that if the storage class is not given for a variable in its declaration, then the compiler will assume a storage class as the default.

A storage class describes the following:

  • The location where the variable would be stored.
  • The default initial value of the variable if it is not initialized
  • The scope of the variable
  • The life time of the variable, i.e. for how long will the variable exist.


Syntax of declaring storage classes is:

 storage_class data_type variable_name;


There are four types of storage classes in C:

        1.  Automatic storage class

        2.  Static storage class

        3.  Register storage class

        4.  External storage class


Automatic Storage Class(Auto):

This is the default storage class for all the variables declared inside a function if there is no storage class mentioned. The variables defined using automatic storage class  are called local variables because they are local to a function where they are defined. By default they are assigned garbage value by the compiler.

The keyword used to declare automatic storage class variable is auto.


Example:

auto float x; 

auto char y= ‘a’;


Features of automatic storage class variable:

Storage place: CPU Memory

Default/ Initial value: Garbage value.

Scope: local to the block or function in which they are defined.

Life time: Within the function


Static Storage Class(Static):

The static storage class instructs the compiler to keep the variable value until the end of program. These variables retain their values throughout the life-time of the program. Like automatic variables, static variables are also local to the function, in which they are defined.

Static variables need to be initialized only once in a program and they are exist until the end of the program.

The keyword used to declare static storage class variable is static.


Example:

static int x;


The features of static storage class variable:

Storage place: CPU Memory

Default/ Initial value: zero

Scope: local to the block or function in which it is defined.

Life time: Preserve the value of the variable between multiple function calls.


Register Storage Class(Register):

Register storage class recommended to the compiler to store the variable in register not in the memory. This is the same as automatic storage class but to provide quick access, they are stored in registers of the CPU. If the data is stored in registers, the operation can be performed more quickly. A value stored in CPU register can always be accessed faster than that of variable stored in memory.

Frequently accessed variables are kept in register. Therefore, if a variable is used at many places in a program, it is better to declare its storage class as register. A good example of frequently used variable is loop counter.

The keyword register is used to declare this storage class variable.


Example:

register int i;


The features of register storage class variable:

Storage place: CPU registers.

Default/Initial value: Garbage value

Scope: local to the function or block in which it is defined.

Life time: within the function


External Storage Class(Extern):

External storage class informs the compiler that the variable defined as extern is defined elsewhere in any program. We use external variable for giving a reference of any global variable which have been already defined.

The external storage class is used when we have global variables which are shared between two or more files. These variables are accessible throughout the program. The variables declared as extern are not allocated any memory.

The keyword used to declare external storage class variable is extern.


Example:

extern int x;


The features of external storage class variable:

Storage place:  CPU Memory

Default/Initial value: Zero

Scope: Global

Life time: Till end of the main program



Post a Comment

0 Comments