×
>
<

Aptitude

Storage Class in C

Storage Classes are used to describe the different features of variables and functions in C programming language.

These features mainly includes the following point.

  • The Scope of Variable.
  • The Lifetime of variable.
  • The location where the variable will be stored.
  • Initial value of Variable.
  • Who can access the variable or where the variable is accessible.
Storage Class Types :

Types of Storage Classes

In C Programming Language, there are four types of storage classes.

  1. Auto Storage Class
    • This is the default storage class for all the variables declared inside a function or a block.
    • There is no need for writing the auto keyword while writing the C program.
    • Example:
    • #include <stdio.h>  
      
      void display() {
          auto int x = 10;
          printf("Value of auto variable x inside function: %d\n", x);
      }
      
      int main() {
          display();
          return 0;
      }
      
  2. Extern Storage Class
    • Extern storage stands for external storage.
    • Extern means that the variable is declared outside the block where it is used.
    • Example:
    • #include <stdio.h>
      
      extern int x;
      
      int main() {
          printf("Value of extern variable x: %d\n", x);
          return 0;
      }
      
      int x = 20;
      
  3. Register Storage Class
    • Register storage class is almost the same as the auto storage class.
    • Register storage basically stores a variable into the CPU register rather than RAM for faster access.
    • Example:
    • #include <stdio.h>
      
      int main() {
          register int x = 30;
          printf("Value of register variable x: %d\n", x);
          return 0;
      }
      
  4. Static Storage Class
    • This storage class is used to declare static variables which are popularly used while writing programs in the C language.
    • Static variables have a property of preserving their value even after they are out of their scope.
    • Example:
    • #include <stdio.h>
      
      void display() {
          static int x = 40;
          printf("Value of static variable x inside function: %d\n", x);
          x++;
      }
      
      int main() {
          display();
          display();
          return 0;
      }
      

These storage classes play a crucial role in determining the scope, lifetime, and visibility of variables in C programs.