Storage Classes in C

In C programming, storage classes define the scope (visibility) and lifetime of variables and functions. They also determine the memory location of variables. There are four types of storage classes in C: auto, register, static, and extern.

Types of Storage Classes in C

  1. auto: Default storage class for local variables.
  2. register: Used to store variables in the CPU register instead of RAM.
  3. static: Retains the value of a variable between function calls.
  4. extern: Used to access global variables declared outside the current file.

Storage Classes in C

Storage Class Storage Place Default Value Scope Lifetime
auto RAM Garbage Value Local Within function
extern RAM Zero Global Till the end of the main program
static RAM Zero Local Till the end of the main program, Retains value between multiple function calls
register Register Garbage Value Local Within the function

1) Auto Storage Class

The auto storage class is the default for all local variables. These variables are automatically created when a function is called and destroyed when the function finishes execution. The auto keyword is optional because it is the default.

Example

                        
#include <stdio.h>
void demo() 
{
  auto int x = 10; // auto is optional
  printf("Value of x: %d\n", x);
}
    
int main() 
{
    demo();
    return 0;
}
                        
                    

Output:

Value of x: 10

2) Register Storage Class

The register storage class is used to store variables in the CPU registers instead of RAM to provide faster access. It is mainly used for frequently accessed variables in performance-critical applications. However, it's not guaranteed that the variable will be stored in a register, as it depends on the system and compiler.

Example

                        
#include <stdio.h>
void demo() 
{
  register int x = 100;  // Store in register
  printf("Value of x: %d\n", x);
}
    
int main() 
{
    demo();
    return 0;
}
                        
                    

Output:

Value of x: 100

3) Static Storage Class

The static storage class preserves the value of a variable between function calls. A variable declared as static retains its value even after the function execution is completed and can be accessed again in subsequent calls. The static keyword can be used for both local and global variables.

Static Variable Example:

Example

                        
#include <stdio.h>
void demo()
{
    static int x = 10; // static variable
    printf("Value of x: %d\n", x);
    x++;
}
    
int main() 
{
    demo();
    demo();
    demo();
    return 0;
}
                        
                    

Output:

Value of x: 10
Value of x: 11
Value of x: 12

4) Extern Storage Class

The extern storage class is used to declare a variable that is defined outside the current file. It allows access to variables and functions declared in other files. The extern keyword does not allocate memory for the variable; it simply tells the compiler that the variable is declared elsewhere.

Extern Variable Example:

Example

                        
// file1.c
#include <stdio.h>
    
extern int x; // Declare extern variable
    
void demo() 
{
    printf("Value of x: %d\n", x);
}
    
 file2.c
#include <stdio.h>
    
int x = 100;  // Define extern variable
    
int main() 
{
    demo();
    return 0;
}
                        
                    

Output:

Value of x: 100