Storage classes in will let us know about the variables' scope, storage, and the initial default value of a variable. In short, the storage classes will describe the feature of a variable or function.

This article by simplilearn will help you understand storage classes in C in detail.

The general syntax of a storage class declaration is:

Storage_class datatype variable_name; 

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Four Types of Storage Classes in C Are:

  1. Auto
  2. Static
  3. Register
  4. Extern 

You will go through each of them in detail.

Auto Storage Class

  • Auto keyword refers to an automatic variable. By default, all local variables are automatic variables.
  • The scope of an auto variable is within the function block and cannot be accessed outside the function.
  • Auto variables are declared at the beginning of the function. Once the function called the memory is allocated, and when the controls come outside the function, the memory of auto variables is de-allocated.
  • By default, the initial value of auto variables is garbage values with no meaning.

Declaration syntax of an auto variable is:

Int a; //by default, it is an auto variable.

Float b;

This is similar to prefixing the keyword auto before the data types.

Auto int a;

Auto float b;

Example Program Using Automatic Variables.

#include <stdio.h>

void main()

 {

   int a = 5; //auto variable by default 

   auto b = 10; 

   printf("a = %d \n", a);

   printf("b = %d \n", b);

 } 

Output:

Storage_Classes_C_1

Now that we know the use of auto variables in C programming, following we have static storage class in C.

Static Storage Class

  • Static variables are declared with the keyword static. The static variable's lifetime is throughout the program run-time.
  • The static variable declared inside the function cannot be accessed outside the function.
  • Suppose the static variables are declared inside the function. Once the function is called, a static variable's memory is allocated, and it won't be de-allocated when the control comes out of the function. Therefore, the current value of a static variable can be accessed in the following function call.
  • Static variables must be initialized before the program execution; otherwise, the initial default value will be zero.
  • Static variables can be declared globally and accessed throughout the program.

Declaration syntax of a static variable is:

static data-type variable_name = value_initialization;

Example:

Static int sum =0;

Example Program Using Automatic Variables.

#include <stdio.h>

int calc() 

{

 static int sum = 2; 

    sum++;

    return sum; 

}

void main()

{

    printf("sum = %d \n", calc());

    printf("sum = %d \n", calc());    

}

Output:

Storage_Classes_C_2

In the above-given program, the value of a static variable is initialized to the value 2. If the static variable is not initialized in the program, then by default, the initial value will be assigned to zero. 

The static variable is local to the function in which it is defined. And once the function is called, the value will be incremented by one that is the initial value 2 + 1 = 3 and print the value 3 

Once the control is out of the function, the memory will not be de-allocated; hence, value 3 is stored. 

In the next function call, the value 3 will be incremented by one, and print the new value that is 4. 

And if the variable were not declared static, then for both the function call, the sum value would be the same; that is, sum = 3 for both the function call.

Up next, we have register storage class.

Become a Data Scientist with Hands-on Training!

Data Scientist Master’s ProgramExplore Program
Become a Data Scientist with Hands-on Training!

Register Storage Class

  • Variable declared with the keyword register are register variables. The scope of register variables is within the function block where it is defined.
  • Register variables are similar to auto variables in C. however; they are stored in the CPU register. Unlike the auto variables stored in the memory 
  • The frequently accessed variables are stored in the CPU register since the access time is less and makes our program run faster.
  • Sometimes, when all the CPU registers are busy, the register variables are automatically stored in the memory, which is identified as an auto variable.

Declaration syntax of a register variable is:

register data_type variable_name;

Example:

register int a;

Example Program Using Automatic Variables.

#include <stdio.h>

int print() 

{

  register int i=0; 

    i++;

    return i;

}

void main()

{

    printf("The value of i = %d \n", print());

}

Output:

Storage_Classes_C_3.

We got clear idea of why we use register storage class in C programming language, next we have extern storage class.

Extern Storage Class

  • External variables are declared with the keyword extern. By default, all the global variables are external variables. The scope of the variable is throughout the program.
  • Extern variables are called global variables since they are declared outside the functions before the main function.
  • External variables share the variables among the multiple C files.
  • External variables are stored in the memory, and their initial default value is assigned to zero.
  • The values assigned to the external variables can be changed in another program block. 

Declaration syntax of a register variable is:

extern data_type variable_name;

Example:

extern float pi;

File 1 (file.h)

extern int x = 10,y = 20;

In the file demo.c let's try to access the external variables 

So for that, let's include the header file demo. h.

File 2 (demo.c)

#include "file.h"

#include <stdio.h> 

void main()

{

    int add;

    printf("x = %d \n",x);

    printf("y = %d \n",y);

    add=x+y; 

    printf("Add = %d \n",add);   

}

From the above-given program, we are trying to access the extern variables in the demo.c file, which is declared in the program file.h

Output:

Storage_Classes_C_4

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Next Steps

"Data Structures in C" can be your next topic. So far, you have learned the storage classes in the C programming language. The following fundamentals will be the data structures and the varieties in data structures used for different purposes.

If you are interested in building a career in the field of software development, then feel free to explore Simplilearn's Courses that will give you the work-ready software development training you need to succeed today. Are you perhaps looking for a more comprehensive training program in the most in-demand software development skills, tools, and languages used today? If yes, our Post Graduate Program in Full Stack Web Development should prove to be just the right thing for your career. Explore the course and enroll soon.

If you have any questions regarding the "Storage classes in C" tutorial, please let us know in the comment section below. Our experts will get back to you ASAP.

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 15 Apr, 2024

6 Months$ 8,000
Full Stack Java Developer

Cohort Starts: 2 Apr, 2024

6 Months$ 1,449
Automation Test Engineer

Cohort Starts: 3 Apr, 2024

11 Months$ 1,499
Full Stack Developer - MERN Stack

Cohort Starts: 3 Apr, 2024

6 Months$ 1,449