Storage Classes in C

Storage classes in C: These are nothing but, which defines the scope and lifetime of the variable or a function in the C program. Here the scope of the variable is nothing but the visibility of the variable.
Syntax: storage_specifier data_type variable _name; 

Storage Classes in C

There are 4 different types of storage classes in C. They are

  1. auto
  2. register
  3. static
  4. extern

1) auto – Storage Class

The storage class in C comes as a default class for all local variables. It is used to define the storage class explicitly.  While the lifetime in this class depends upon the variable.

Example

#include<stdio.h>                     
void increment(void);                    
int main() {                       
  increment();                     
  increment();                     
  increment();                     
  increment();             
return 0;          
 }             
void increment(void)   {                  
auto int i=0;                  
printf("%d ",i);                     
i++;             
}

Output: 0 0 0 0

2) register – Storage Class

The register storage class in C is used for classifying local variables whose value needs to be saved in a register in place of RAM. When you want your variable the maximum size, it uses the keyword register. Only variables with limited size can be used because they can be having only less space. (i.e. 16bits, 32bits, 64bits)

Syntax: register datatype variable; 

Example

#include <stdio.h>                  
int main()  {                   
register int i;               
int arr[5];// declaring array                 
arr[0] = 10;// Initializing array                 
arr[1] = 20;               
arr[2] = 30;               
arr[3] = 40;               
arr[4] = 50;           
for(i=0;i<5;i++) { //Accessing each variable               
printf("value of arr[%d] is %d \n", i, arr[i]);            
}             
return 0; }

 Output
value of arr[0] is 10
value of arr[1] is 20
value of arr[2] is 30
value of arr[3] is 40
Value of arr[4] is 50

3) Static – Storage Class

These static variables are used popularly for writing programs in C language. And the  Static variables store the value of a variable even when the scope limit exceeds. Static storage classes in C has its scope local to the function in which it is defined. And the static modifier may also be applied to global variables. When this is done, it causes that variable’s scope to be restricted to the file in which it is declared.

Example

#include<stdio.h>                          
void increment(void);                          
 int main()  {                               
increment();                              
increment();  
increment();                              
increment();                    
return 0;  
}               
void increment(void) {                   
 static int i = 0;                  
printf ("%d ", i ) ;                       
i++;                    
}

Output: 0 1 2 3

4) extern – Storage Class

The scope of the extern variable is available throughout the program. They are similar to the Global variable. And the extern variable has the facility that it can be present anywhere in the program. When you have multiple files and you define a global variable or function, which can also be used in other files, then the extern will be used in another file to provide the reference of defined variable or function.

Example

#include<stdio.h>                    
int x = 10;  
int y=50;                  
int main( ) {                     
extern int y;                     
printf("The value of x is %d \n",x);                     
printf("The value of y is %d",y);                  
return 0; }

Output
The value of x is 10
The value of y is 50