What are Storage Classes in C | Programming Tutorials


C Language
Storage Classes in C Language
What is Storage Class in C Language? Learn C programming Online.

In C language, variables have not only datatypes but also storage classes. Storage class provides information about the locations of variables and their visibility. It is Storage class which decides the portion of the program within which variables are recognized.
To know more about storage classes, take a look at following example:
You May Also Like: Loops in C Language

/* Examples of storage class */
int X;
main()
{
int Y ;
float value;
..................
..................
function1();
}
function1()
{
int Y;
float sum;
................
................
}


In above example , The X variable is declared before main function , so this is called global variable. (Global Variables are those variables which are used in more than one function or in multiple functions. These variables are declared out of all functions i.e. in global declaration section.)
Now this variable can be used in all functions. There is no need to declare it again and again when using it in other functions. Such variable is known as external variable.

The other variables Y, float and sum are known as local Variables. They are called such because they are declared inside a function. Local variables can be used only in that function in which they are declared. We can't use them in other functions. If we need to use them in other functions then we have to declare it again in that function. Please take a look at the example in which we declared Y two times. Now any change in one variable does not affect other variable used in other function.
You May Also Like: Antivirus Selection Criteria
C language provides a number of storage class specifiers that can be used to declare scope and lifetime of a variable. The Storage Class Specifiers are of four types as shown below:
  • Automatic Variables
  • Static Variables
  • External Variables
  • Register Variables 
There explanation is given below:
Automatic Variables: The memory space is given to the automatic variables when they enter into block of function and is de-allocated when the function block exits. In simple words we can say that "In automatic variables local variable can be used only in that function in which it is declared.
Note: If we doesn't specify any storage class specifier, then Auto is automatically set by default.
Static Variables: In Static variables, memory space is allocated to variable when the execution of program began and de-allocated when execution stops. In simple words, we can say that,"In Static variables , local variable which exists and retains its value even after the control is given to other function.

External Variables: When we declare the variable outside the main function, like we did in the example, then such variables are known as External Variables. They are also known as Global variables. We need to declare these variables only one time when writing program and can use it in any function.

Register Variables: In C , if some variables are repeatedly used, then we can save them in registers of Microprocessors. By doing this, we can have access to these variables immediately. Thus we can do work more fast.
You May Also Like: Play Android Games on PC

Functions of Storage Classes in C Language: 
  • Tells about the Storage location of variables.
  • Tells about the lifetime of variables.
  • Declares scope of variables.
  • Gives starting value of variables.
  • If starting value is not specified, then sets it to default.