What is storage class and its types ?

5 min 42 sec read Basic

Storage classes in C provide the way how variables can be stored in the memory. The concept of 'local variables' and 'global variables' derives from this. Storage classes refer to the scope of a variable that defines the lifetime of the variable.  Storage classes are also useful to define the scope or visibility, and the initial value of the variable.

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

What is auto storage class in C?

The auto keyword is the default storage class for all the local variables declared inside a block or function. auto variables can be accessed only within the block of the function and not outside that block where its scope is not defined.

auto storage class program

void main()
  int b = 5;
  // Same as above.Auto keyword is default keyword. 
  auto int a = 5;   
  printf(" %d, %d ", a,b);


5 , 5

Auto keyword is present by default. No matter whether you write it or not. It stores the value in RAM.

What is static variable in C?

Static variables in C are initialized only once in a lifetime of the program. That means it gets initialized only once when the function is called and its lifetime is throughout the program run. Static variables are declared using a static keyword. It holds a default value of zero.


void function()
/* This statement gets initialised only once
 then it is ignored. */
    static int a = 0; 
    int b = 0;
    printf("a = %d , b = %d",a,b);

int main ()
    function ();
    function ();
    function ();
    return 0;


a = 1 , 
b = 1
a = 2 , 
b = 1
a = 3 , 
b = 1

At first main() function is called. After the first execution of "function()" static variable 'a' becomes 0. Then it increments it's value by 1. After the second execution of "function" static variable 'a' value is 1 not 0. Because static variables in C gets initialised only once in a lifetime of the program. In third execution of "function()" value of static variable becomes '3' .

What is register storage class in C?

register variables are mostly used in places where we need fast execution. Because it is stored in the register and registers are always faster than RAM. It is mostly used in incrementing the counter and mostly used in loops.

register int counter=0;  

What is extern storage class in C?

A variable declared with an extern keyword defines that your code wants to access the variable declared with the same name stored in another source file. Extern storage specifier is used to extend the visibility of variables/functions

Consider this example for a better understanding of the extern keyword.


Create two files Demo.c and NewDemo.c In Demo.c

#include <stdio.h>
#include "NewDemo.c" 

 int num =  75 ;  
 extern void display();

int main()
 return 0;

In NewDemo.c

#include <stdio.h>

extern int num;
void display()
 printf("Num is = %d
",num );

Output after compiling and running demo.c,

Num is = 75

The entry point in demo.c is the main function.

Inside main function we first called the display() function. This display() function is not defined in demo.c.

display() function is defined in NewDemo.c file.

So you have to tell the compiler that the display() function is defined somewhere else and I have to use it. So declare extern void display() before the main function in the demo.c This extern keyword tells the compiler that it's okay if this program demo.c does not contain the definition of display() function. I will search it somewhere else. Just provide me the name of the file where it is being defined.

In this case, we included #include “NewDemo.c”

Now in NewDemo.C

display() function is defined here .

This function prints the value of num. But if you see num is not initialised with any value in this program. Yes, if you see in NewDemo.c, num is not defined anywhere.

So you have to tell the compiler that this variable num is defined somewhere else. So declare this variable as extern int num at top of program outside the function.

In this way,

In this case, we included #include “newDemo.c” in demo.c

I hope you got the concept of auto, register, static, and extern storage classes in C. Below you can find the related articles in C programming. Check them out also.

How was the tutorial? Nice. Right?

Deep Concept videos to crack the highly-piad interviews.

Chekout out our Youtube Channel to get detailed video content on important topics in interviews.

What is your Interview Score?

Test your skillset with the curated questions created by experts around the globe.

Book a free test slot. Now !.

It usually takes 30 mins for an online test and this test will be MCQ based. Get detailed analytics based on your test and recommedations with personalised roadmaps.

Book a test slot. Now!

Have questions about the trial coding class?

Chat with our experts to discuss

Connect on WhatsApp

Recommended tutorials

#online compiler for c     #python for programming    

#dfs and bfs algorithm     #programming with c language

#storageclass in C    #listcomprehension in python