Online Classes

Results can only be achieved if you have the focused direction and crystal clear knowledge. To achieve this, you need a mentor. We will help you out by connecting with an expert mentor in the field.

Learn More

Article

What are the different types of storage classes in C programming ?

5 min 8 sec read

What are Storage Classes?

Storage Classes in C provides the way how to store variables in memory. There are four types of storage classes in C programming. 

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

The auto Storage Class

The auto keyword is the default storage class for all the local variables. auto keyword is also known as automatic variable. It's default value is Garbage Value.

Example:-

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

Output:-

5 , 5

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

The Static Storage Class

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. It's default value is zero.

Example

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

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

Output

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' .

The register storage class

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;  

The extern storage class

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

Example:-

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

#include 
#include "NewDemo.c" 

 int num =  75 ;  
 extern void display();

int main()
{
display();
 return 0;
}

In NewDemo.c

#include 

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 initialized 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,

extern is used to telling the particular program that a variable or function is not present in this program. So search it somewhere else. But do not forget to include the name of the file where that extern variable or function is defined.

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.

Related Articles

1) Write C programs to implement i) strcpy(), ii) strlen() library function by yourself

2) Write C program to insert a node after a given node in a linked list

3) How to use %5.2f format specifier in C Programming?

4) Question on C Program referencing the Increment and Decrement Operators concept asked in GATE CSE

5) Article on different storage classes in C.

6) C Tutorial on Calloc() vs malloc() functions is explained in depth in this article.

Try to execute what you have learnt

Easy to use online data structure compiler where you can execute the programs in your favourite programming language.
(C, C++, Python)

Open Compiler

HTML, CSS and Javascript Real time Web Editor

Execute your HTML, CSS and javascript code in real time with the web editor
(HTML, CSS, Bootstrap, Javascript)

Open Web Editor