Storage Classes in C

The values given to the variables are stored in the computer locations.
These locations can be called the storage class in C.
There are  two kinds of computer locations where the variable values may be kept.
1.      Memory
2.      CPU registers.

Basically, there are four storage classes in C
a.      Register storage class
b.      External storage class
c.      Automatic storage class
d.      Static storage class

Register Storage Class

The variable value is stored in the CPU register. 
The variable value when stored in CPU register has fast accessibility. 

The "register" Keyword is used to declare an register value. 
Syntax:
register int i;

Even though the variable is declared using register keyword, it can not surely said that the variable is in CPU register. The reason behind this is, there are limited CPU registers, and they may be busy doing some other task. In such a condition, the variable works as if its storage class is auto.

The variable value stored in CPU register using "register" keyword will never show the address of the variable. If we try to access the address of the variable it will show an compilation error of " Address of register variable requested".
Example:
#include<stdio.h>
int main() 
  register int i;
  printf("%p", &i);
 return 0;
}
Output: error

The register variable can be declared only  locally inside the function. 
If we try and to declare it globally, it will show an complilation error of "Illegal storage class on file-scoped variable ".
Example:
#include<stdio.h>
  register int i;
  int main() 
{
  printf("%d", i);
 return 0;
}
Output: error

The default value of register variable is garbage value. 
#include<stdio.h>
  int main() 
{
  register int i;
  printf("%d", i);
 return 0;
}
Output: garbage value

We can not declare register storage class variable with any other storage class variable. 
#include<stdio.h>
  register auto int i=10;
  int main() 
{
  printf("%d", i);
 return 0;
}
Output: error



The lifetime of register variable is till the execution of the block or function in which it is declared.

We can do the operations with register storage class and other storage class together. 
Example:
#include <stdio.h>
int main() 
{
    register int i=10;
    static int j=20;
    printf("%d",i+j);
    return 0;
}
Output: 30
  

External storage class

The variable value is stored in memory.  It is a global storage class i.e, it is declared outside all the functions and can be used by any function in the program.
Example::
                    Consider there are two files in a program file1 and file2. A global variable is declared in file1. This global variable can also be used in file2 using the extern keyword.

File1
File2
#include<stdio.h>
int i=10;
void fun();
  int main()
{
 fun();
fun();
printf("%d",i);
return 0;
}

void fun()
{
 i=i+1;
}
Output= 12

extern int i;
void fun();
  int main()
{
 fun();
fun();
printf(“%d”,i);
return 0;
}

void fun()
{
 i=i+1;
}
Output=14
 
  Below is small example on the extern keyword. In this program an int variable i is declared twice. 
In first turn it is declared as a normal variable, and in second turn it is declared as a extern keyword.
#include<stdio.h>
 int i=10;
void fun();
  int main()
{
 fun();
fun();
printf("%d",i);
extern int i;
fun();
fun();
printf("\n%d",i);
return 0;
}

void fun()
{
 i=i+1;
} 
 
output:
12
14 

 
To know about automatic and Static storage class, follow the below

 
storage-class-c