Storage classes in 'C'

A variable in C programming has two properties : 
1. Type. 
2. Storage class
Type of variable refers to the data type of variable whether it is an integer or floating point value etc. Whereas the storage class in C tells us where the variable would be stored, what will be the initial value of the variable if no value is assigned to that variable, life time of the variable and scope of the variable ( whether local or global ).

There are four storage classes in C :

1. Automatic storage class      Keyword : auto
2. Register storage class         Keyword : register
3. Static storage class              Keyword : static
4. External storage class        Keyword : extern


1. Automatic storage class

Storage Specifier       : auto   
Storage place            : CPU Memory
Initial / default value  : Garbage value
Scope of variable      : local
Life of the variable    : only Within the function

Program to demonstrate the use of AUTO Storage Class :

#include<stdio.h>
void auto_increment();

int main()
{
   auto_increment();
   auto_increment();
   auto_increment();

   return 0;
}

void auto_increment()
{
   auto int i = 0 ;
   printf ( "%d ", i ) ;
   i++;
}

OUTPUT :  0  0  0


2. Register storage class

Storage Specifier       : register   
Storage place             : Register memory
Initial / default value  : Garbage value
Scope of variable      : local
Life of the variable    : Within the function

Program to demonstrate the use of REGISTER Storage Class :

#include <stdio.h>

int main()
{
   register int i;
   int register_array[4];

   register_array[0] = 10;
   register_array[1] = 20;
   register_array[2] = 30;
   register_array[3] = 40;

   for (i=0;i<4;i++)
   {
     printf("value of register_array[%d] is %d \n", i, register_array[i]);
   }

   return 0;
}


OUTPUT: 



3. Static storage class

Storage Specifier       : static   
Storage place            : CPU Memory
Initial / default value  : Zero
Scope of variable      : local
Life of the variable    : It retains the value of the variable between different function calls

Program to demonstrate the use of STATIC Storage Class :

#include <stdio.h>
void test();
int main()
{
   test();
   test();
   test();
}
void test(){
    static int a=0;
    printf("%d\t",a);
    a+=10;
}

OUTPUT:  0  10  20


4. External storage class

Storage Specifier       : extern  
Storage place            : CPU Memory
Initial / default value  : Zero
Scope of variable      : Global
Life of the variable    : Till the end of the main program. Variable definition might be anywhere in the C program

Program to demonstrate the use of External Storage Class :

#include<stdio.h>
void ext();
int a=10; 
/* here a is global variable because it is declared outside every function */

int main()
{
    a=a+6;
    ext();
    return 0;
}

void ext()
{
  ++a; 
 
/* a is not declared in this function but, it still works in this function as it is declared as a global variable */

  printf("a=%d\n",a);
}


OUTPUT :  a=17