Storage Class


Declaration of Storage Class

C variables may contain not only data types but also storage classes that provide information about their location and visibility. The storage class determines the part of the program in which the variables are recognized. Consider the following example:

Program
/* Example of storage classes */
int a;
int main()
{
 int c;
 float bal;
 .....
 Addition();
 return 0;
}
Addition()
{
 int b;
 float sum;
 ....
}

The variable 'a' which has been declared before the main() method is called the global variable. It can be used in all the methods in the program. It need not be declared in other methods. A global variable is also known as an external variable.

The variables b,c, bal, and sum are called local variables because they are declared inside a method. Local variables are visible and meaningful only inside the methods in which they are declared. They are not known to other methods.

C specifies different storage classes that can be used to clearly declare the scope and lifetime of variables. Opportunity and lifetime concepts are only important in multi-function and multiple file programs and therefore storage classes will be considered in detail later after discussing the functions.

The storage class is another selector that can be added to a variable declaration as shown below:


Storage Classes and Their Meaning

Storage Class Meaning
auto The local variable is known only to the method in which it is declared. Default is auto.
static The local variable exists and retains its value even after the control has been transferred to the calling method.
extern The global variable is known to all methods in the file.
register The local variable is stored in the register.

Note : static and extern variables are automatically initialized to zero. auto variables contain undefined values (known as 'garbage') unless they are initialized explicitly.