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);
}

Output:-

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.

Example

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

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.

Example:-

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()
{
display();
 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.

Codzify's

1:1 & 1:Many Mentorship Program

Be job-ready. Learn how to crack the interviews with industry ready training program.


Learn More

Data Structures & Algorithms

What is Dynamic Programming and how to use it

This is a quick introduction to Dynamic Programming.

3 ways to check anagrams

Let's discuss the 3 ways to check anagrams and their time complexity.

Bubble Sort Algorithm

Topics covered: 1) Basics of Bubble Sort Algorithm 2) Worst Case, Best Case complexity

Introduction to Trees

What is a Tree and Terminologies like root, sibling, parent node, children, Leaf node, Ancestor & Descendant, Properties of Tree Data Structure, Depth of node, Height of node

Address Calculation in 2d array

Let's discuss about the Address Calculation using row-major order & column-major order.

Javascript

Javascript - Postfix Vs Prefix

Let's discuss the difference between postfix and prefix with a practical example.

Javascript Identifiers, Variables

In this session, we will discuss the concept of identifiers, variables and operators in javascript with a practical example.

Bootstrap

Bootstrap 4 Tutorial: Modal Popups Made Easy | Live Demo

Let's discuss the Modal Popups in Bootstrap.

How to create a dropdown list in navbar bootstrap

In this video, we will discuss How to create a dropdown list in navbar bootstrap with a practical example.

Navigation tabs in Bootstrap

Let's discuss about the navigation tabs in Bootstrap

Bootstrap Buttons

Let's discuss about the button classes in bootstrap.


Recommended tutorials