Storage classes in C

In this tutorial we will learn about Storage classes in C. The storage class defines the visibility and the life-time of a variable or a function during the execution of a program. There are total four storage classes available in C language which are as follow.

Available Storage classes in C:

  • auto
  • register
  • static
  • extern

To understand about these storage classes in C please check out the following table.

Name Storage Area Initial Value Scope Life-time
auto stack garbage within block end of block
register CPU register garbage within block end of block
static Data Segment zero within block end of program
extern Data Segment zero global multiple files end of program

 

auto:

The auto is default storage class for all the local variable(variables inside a function or block). All the auto variables can be only accessed within the block. In case you want to access auto variables from outside of scope you can use pointers(we will learn about pointers in upcoming tutorials). Here is an example program to understand auto variables more wisely.

Example Program:

#include <stdio.h> 
void show() 
{ 
    auto int a = 50; 
    int b=10;
    // printing the auto variable a and b 
    printf("Here variable a and b both are auto %d  %d", a ,b); 
} 

int main() 
{ 
    // calling show method
    show(); 
    return 0; 
} 

register:

The register variables work same as auto variables the only difference is compiler store register variables in the register of the microprocessor if there is any free register. The register variables work faster than auto variables because it stores in the register. It is important to note the scope of the register variable is within the block. Please check out the following example program to understand it more wisely.

Example Program:

#include <stdio.h> 
void show() 
{ 
    register int a = 50; 
    // printing the register variable a 
    printf("Here variable a =%d", a); 
} 

int main() 
{ 
    // calling show method
    show(); 
    return 0; 
} 

static:

The static variables are different than all other kind of variables because static variables can maintain their values between function calls. The static variables only initialize once and then exists until the end of the program. Please check out the following program to understand static variables.

Example Program:

#include <stdio.h> 
void show() 
{ 
   static int i = 10; /*static varible */
   int j=10; /*auto variable*/
   i++;
   j++;
   printf("i is %d and j is %d \n", i, j);
} 

int main() 
{ 
    for(int i=0;i<5;i++){
      //calling show method
      show();
    }
} 

extern:

The extern storage class is use to create global variables which visible to all the program files. It is so helpful to create global variables when two or more program files use same variable. To understand extern variable please checkout the following example program.

Example Program:

#include <stdio.h> 
int num;
void show() 
{ 
   extern int num; 
   printf("Value of num %d\n",num); 
    // modifying value of num variable 
    num = 2; 
    // printing num variable
    printf("New value of num %d\n", num); 
} 

int main() 
{ 
      show();
} 

 

Spread the love
Scroll to Top
×