In this article, we provide complete information about Storage Classes in C, which define the scope, lifetime, and visibility of variables and functions.

Storage Classes in C Programming:

A storage class is essentially a keyword used before the declaration of a variable or function to determine where and how the variable will be stored, its default value, scope, and lifetime.

Introduction to Storage Classes

Storage classes in C change how a variable or function behaves in a program.
They decide:

  • The location of the variable in memory (e.g., RAM or CPU register).
  • The lifetime (how long the variable exists).
  • The scope (where it can be accessed).

There are four main types of storage classes in the C language:

  1. Automatic Storage Class (auto)
  2. External Storage Class (extern)
  3. Register Storage Class (register)
  4. Static Storage Class (static)

1 Automatic Storage Class:

The Automatic Storage Class is the default storage class for all local variables.
Whenever you create a variable inside a function without specifying any storage class, it is automatically treated as an auto variable.

Characteristics:

  • Declared using the keyword auto
  • Visible only within the function where declared (local scope)
  • Destroyed automatically after the function ends
  • Default value: Garbage
  • Stored in RAM

Syntax:

auto data_type variable_name = value(optional);

Example:

#include <stdio.h>

int main() {

    auto int a;

    auto int b = 15;

    printf(“Value of a : %d\n”, a);   // garbage value

    printf(“Value of b : %d”, b);

    return 0;

}

Output:

Value of a : 1568  // garbage value

Value of b : 15

2 External Storage Class

The External Storage Class allows variables to be shared between multiple files in a project.
If a variable is declared outside all functions, it becomes global.
To access such global variables in other files, the extern keyword is used.

Characteristics:

  • Declared using extern keyword
  • Global scope — accessible throughout the program
  • Default value: 0
  • Stored in RAM
  • Lifetime — entire program duration

Syntax:

extern data_type variable_name = value(optional);

Example:

#include <stdio.h>

int num = 15;   // Global variable

void func();    // Function declaration

int main() {

    extern int num;   // Accessing global variable

    printf(“Value of num : %d\n”, num);

    func();           // Function call

    return 0;

}

void func() {

    extern int num;   // Reusing same global variable

    printf(“Value of num : %d”, num);

}

Output:

Value of num : 15

Value of num : 15

3 Register Storage Class

The Register Storage Class is used to store variables in the CPU registers instead of RAM for faster access.
Registers can hold only small data values, but they are much faster than memory locations.

Characteristics:

  • Declared using the keyword register
  • Local scope — accessible only within the function
  • Stored in CPU register (if available)
  • Default value: Garbage
  • Cannot access memory address using & operator

Syntax:

register data_type variable_name = value(optional);

Example:

#include <stdio.h>

int main() {

    register int num = 15;

    printf(“Value of num : %d\n”, num);

    // printf(“%d”, &num); // Error: Cannot access address of register variable

    return 0;

}

Output:

Value of num : 15

4 Static Storage Class

The Static Storage Class ensures that a variable retains its value between multiple function calls.
A static variable is initialized only once and exists till the end of the program.

Characteristics:

  • Declared using the keyword static
  • Can have local or global scope
  • Default value: 0
  • Stored in memory (not destroyed after function exit)
  • Lifetime — entire program

Syntax:

static data_type variable_name = value(optional);

Example:

#include <stdio.h>

void numFunction();

int main() {

    numFunction();

    numFunction();

    numFunction();

    return 0;

}

void numFunction() {

    static int num = 0;  // Static variable

    num = num + 2;

    printf(“%d\n”, num);

}

Output:

2

4

6

Explanation:
Each time numFunction() is called, the variable num retains its previous value.
It is not reinitialized with zero after each function call, as happens with automatic variables.

Comparison of Storage Classes in C

Storage Class Keyword Scope Lifetime Default Value Memory Location
Automatic auto Local Within function Garbage RAM
External extern Global Entire program 0 RAM
Register register Local Within function Garbage CPU Register
Static static Local/Global Entire program 0 Memory

Conclusion

Storage Classes in C play an essential role in managing variable lifetime, visibility, and memory usage.
They help programmers control how and where data is stored, improving program performance, memory optimization, and code organization.

In short:

  • Use auto for local variables (default).
  • Use extern for global variables across files.
  • Use register for fast access.
  • Use static to preserve variable values between function calls.

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *