Storage Classes in C with Examples

Storage Classes in C with Examples Auto, Register, Static, Extern (1)

In C programming, storage classes define how variables and functions behave in terms of memory allocation, scope, lifetime, and linkage. Every variable in a C program has a storage class, either assigned automatically by the compiler or explicitly declared by the programmer.

Understanding storage classes in C is extremely important for writing optimized, efficient, and bug-free programs. These concepts are widely used in embedded systems programming in C, operating systems, drivers, firmware development, and performance-critical applications.

The four main storage classes in C are:

  • auto storage class in C
  • register storage class in C
  • static storage class in C
  • extern storage class in C

This tutorial explains all types of storage classes in C with examples, memory behavior, linkage rules, and real-world usage.

Storage classes in C define the scope, lifetime, memory allocation, and visibility of variables in a program. The four storage classes — auto, register, static, and extern — help developers manage memory efficiently and control variable access. Understanding storage classes is essential for C programming, embedded systems, and performance optimization.

What Are Storage Classes in C?

Storage class specifiers in C determine:

Storage classes help the compiler decide:

  • Where the variable should be stored
  • How long it should remain in memory
  • Which parts of the program can access it

 

registor_now_P

Types of Storage Classes in C

There are four storage classes in C:

  • Auto
  • Register
  • Static
  • Extern

1. Auto Storage Class in C

The auto storage class is the default storage class for local variables in C.

Variables declared inside a function are automatically treated as auto variables in C unless another storage class is specified.

Features of Auto Storage Class

  • Scope: Local to the block or function
  • Lifetime: Exists until the block exits
  • Memory: Stack memory in C
  • Linkage: No linkage
  • Default value: Garbage value

Syntax

auto int num;

Example of Auto Storage Class in C

#include

int main()
{
    int a = 10;          // auto by default
    auto int b = 20;

    printf("%d %d", a, b);

    return 0;
}

Output

10 20

Important Notes

  • Auto variables are created when the function starts.
  • They are destroyed after function execution ends.
  • Returning the address of an auto variable causes undefined behavior because stack memory is released after the function returns.

Auto vs Static Variable in C

FeatureAuto VariableStatic Variable
MemoryStackData Segment
LifetimeTemporaryEntire program
Value RetentionNoYes
ScopeLocalLocal/Global

2. Register Storage Class in C

The register storage class tells the compiler to store the variable in a CPU register instead of RAM whenever possible.

Register variables in C are mainly used for fast access operations such as loop counters and delay variables.

Features of Register Storage Class

  • Scope: Local
  • Lifetime: Till block execution
  • Memory: CPU Register or Stack
  • Linkage: No linkage
  • Faster access compared to normal variables

Syntax

register int i;

Example of Register Storage Class in C

#include

int main()
{
    register int i;

    for(i = 0; i < 5; i++)
    {
        printf("%d\n", i);
    }

    return 0;
}

Important Rules

  • The compiler may ignore the register request.
  • You cannot use the address operator & on register variables.

Invalid Example

register int x = 10;

printf("%p", &x);   // Error

Why Register Variables Are Used

In embedded systems programming in C, register variables are often used inside delay loops and hardware-related operations where execution speed matters.

3. Static Storage Class in C

The static storage class in C preserves variable values between function calls.

Static variables in C are initialized only once and remain in memory throughout program execution.

Memory is allocated in the data segment in C.

Static Local Variables in C

A static local variable retains its value between function calls.

Features

  • Scope: Inside function only
  • Lifetime: Entire program execution
  • Memory: Data Segment or BSS Segment in C
  • Linkage: No linkage

Example of Static Local Variable

#include

int counter()
{
    static int count = 0;

    count++;

    return count;
}

int main()
{
    printf("%d\n", counter());
    printf("%d\n", counter());
    printf("%d\n", counter());

    return 0;
}

Output

1
2
3

Explanation

Unlike auto variables, static variables do not reset after function execution.

Static Global Variables in C

Static global variables are accessible only inside the file where they are declared.

They provide internal linkage in C.

Example

#include

static int visitors = 0;

void visit()
{
    visitors++;

    printf("Visitors = %d\n", visitors);
}

int main()
{
    visit();
    visit();
    visit();

    return 0;
}

Output

Visitors = 1
Visitors = 2
Visitors = 3

Static Keyword in C

The static keyword in C is also used with functions.

A static function can only be accessed inside the same source file, which improves encapsulation.

Memory Sections Used by Static Variables

Initialized Static Variables

Stored in the initialized data segment.

Uninitialized Static Variables

Stored in the BSS segment in C.

Example:

static int x;   // Stored in BSS segment

4. Extern Storage Class in C

The extern storage class in C is used to share variables across multiple files.

Extern variables in C are declared using the extern keyword in C.

Explore Courses - Learn More

The variable must be defined in exactly one source file.

Features of Extern Storage Class

  • Scope: Global
  • Lifetime: Entire program execution
  • Memory: Data Segment
  • Linkage: External linkage in C

Example of Extern Storage Class in C

file1.c

int num = 100;

file2.c

#include

extern int num;

int main()
{
    printf("%d", num);

    return 0;
}

Compilation

gcc file1.c file2.c

Output

100

Declaration vs Definition in C

Understanding declaration vs definition in C is essential while using extern variables.

Declaration

A declaration tells the compiler about the variable type.

Example:

extern int x;

No memory is allocated here.

Definition

A definition allocates memory for the variable.

Example:

int x = 10;

Memory allocation happens here.

Linkage in C Programming

Linkage defines whether variables or functions can be accessed across multiple files.

Types of Linkage in C

1. External Linkage in C

Global variables without static have external linkage.

Example:

int num = 10;

Accessible from other files using extern.

2. Internal Linkage in C

Variables declared with static have internal linkage.

Example:

static int count;

Accessible only within the same file.

3. No Linkage

Local variables have no linkage.

Each declaration represents a unique entity.

Example:

void fun()
{
    int x = 10;
}

Scope and Lifetime in C

Understanding scope and lifetime in C is critical for proper memory management.

Storage ClassScopeLifetimeMemory
autoLocalBlock executionStack
registerLocalBlock executionRegister/Stack
staticLocal/GlobalEntire programData Segment
externGlobalEntire programData Segment

Storage Classes in Embedded C

Storage classes in embedded C are extremely important because embedded systems have limited memory and hardware resources.

Common Uses

  • register variables for fast execution
  • static variables for persistent states
  • extern variables for cross-module communication
  • auto variables for temporary operations

Embedded firmware developers use storage classes carefully to optimize RAM and performance.

Translation Unit in C

A translation unit in C refers to a source file after preprocessing.

Static global variables and static functions are limited to the current translation unit.

This prevents accidental access from other files.

Variable Declaration in C

Examples of variable declaration in C:

extern int x;

int y;

Variable Definition in C

Examples of variable definition in C:

int x = 10;

static int count;

Storage Classes Summary Table

Storage ClassScopeLifetimeMemory AllocationLinkage
autoLocalBlock executionStackNone
registerLocalBlock executionCPU RegisterNone
staticLocal/GlobalEntire programData SegmentInternal/None
externGlobalEntire programData SegmentExternal

Conclusion

Storage classes in C control how variables behave in terms of scope, memory allocation, lifetime, and linkage. A proper understanding of auto, register, static, and extern storage classes helps developers write efficient and maintainable programs.

Whether you are learning C programming concepts, working on embedded systems programming in C, or preparing for interviews, mastering storage classes is essential.

Understanding concepts such as scope, lifetime and linkage in C, stack memory in C, data segment in C, declaration vs definition in C, and variable memory management will improve your ability to design optimized software systems.

Talk to Academic Advisor

FAQs

Storage classes in C define the scope, lifetime, memory location, and visibility of variables and functions. The four main storage classes are auto, register, static, and extern.

A static variable has internal linkage and is accessible only within the same file, while an extern variable has external linkage and can be accessed across multiple source files.

Storage classes help manage memory efficiently, control variable accessibility, improve program performance, and are especially important in embedded systems programming and large-scale C applications.

Author

Embedded Systems trainer – IIES

Updated On: 09-05-26


10+ years of hands-on experience delivering practical training in Embedded Systems and it's design