Storage Classes in C with Examples

Storage Class refers to the manner in which the memory is allocated by the compiler to variabls. The storage class determines the scope and lifetime of a variable.

The storage class of a variable determines
  1. where it is stored
  2. its default initial value
  3. scope of a variable
  4. lifetime of a variable

Automatic Storage Class

This is the default storage class of variables that are declared within a bock or function. In order to explicitly declare a variable which belongs to automatic storage class, auto keyword is used.

Syntax:
auto data_type variable_name;
Example
auto int i;
These variables are created only when the function (where they are defined) is called and are destroyed when the function ends.

Points To Remember
  1. Storage - Main Memory.
  2. Scope - local to the block where it is defined i.e block scope.
  3. Lifetime - It exists as long as the control remains in the block where it is defined.
  4. Default Initial Value - Garbage.
Consider the example below to understand it better,
#include<stdio.h>

void main()
{
 auto int i=10;
 { 
 auto int i=20;
 printf("%d \n",i);
 }
 printf("%d \n",i);
}
In above example, the two variables 'i' are different since they are defined in two different blocks. Hence the output of above code is

Show Output:
20
10

Static Storage Class

Variables defined with automatic storage class (local variables) do not retain their values between function calls. We can achieve this by using Static Storage Class.

Syntax:
static data_type variable_name;
Example
static int i;

Types of static variables

  1. Local static variables: These variables have function or block scope. They retain their values between calls to the function.
  2. Global static variables: These are global to the file. They are not destroyed. These static variables are visible to all functions of its own file (the file in which it is defined).
Example:
#include<stdio.h>

void increment(); // Function prototype

void main()
{
 int i;

 for(i=0;i<5;i++)
  increment(); 
}

void increment()
{
static int c=1; // Static declaration of a variable

printf("%d ",c++);
}
Output:
1 2 3 4 5
When increment() function is called at first it created a static variable c with value 1. It prints the value and then increment as well. Now c=2.

For the second time when the increment() is called it prints value of c=2. It does not declare c again. And also c is incremented to c=3.

Similarly for the rest of the calls it does the same.

Point to remeber
  1. Storage - Main Memory
  2. Scope - Block or file scope depending upon where it is defined
  3. Lifetime - Persists (retains it value) between function calls if the scope is block scope.
  4. Default Initial Value - Zero

Extern Storage Class

Variables defined with this class are also called as global or external variables. If the program extends over two or more files, extern keyword is used to declare a varibale which can be accessed in both the files.

Syntax:
extern data_type variable_name;
Example
extern int i;

Points to remember
  1. Storage - Main Memory
  2. Scope - File Scope
  3. Lifetime - It exists as long as the program which uses it is running. It also retains the value between function calls.
  4. Default Initial Value - Zero

Example:
#include<stdio.h>

int i=5;

void main()
{
 extern int n;
 void display(void); 
 printf("%d \n",n);
 display(); 
}

void display(void)
{
 extern int n;
 printf("%d \n",n);
}
The declaration of variable with extern class indicates that the function uses an external variable which is defined elsewhere. If both the functions are in same source code file, the declarations are not required.

If we want to use variable n in functions which are defined in separate files we have to use extern keyword.

Register Storage Class

If we want to store a variable in CPU register rather than the main memory. The register variables have similar features as of automatic storage class except the storage location.

Syntax:
register data_type variable_name;
Example
register int i;

Points to remember
  1. Storage - CPU registers.
  2. Scope - Block scope.
  3. Lifetime - Exists as long as the control is within the block where it is defined.
  4. Default Initial Value - Garbage.