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

Types of Storage Classes in C
There are four storage classes in C:
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
| Feature | Auto Variable | Static Variable |
|---|
| Memory | Stack | Data Segment |
| Lifetime | Temporary | Entire program |
| Value Retention | No | Yes |
| Scope | Local | Local/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.

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 Class | Scope | Lifetime | Memory |
|---|
| auto | Local | Block execution | Stack |
| register | Local | Block execution | Register/Stack |
| static | Local/Global | Entire program | Data Segment |
| extern | Global | Entire program | Data 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 Class | Scope | Lifetime | Memory Allocation | Linkage |
|---|
| auto | Local | Block execution | Stack | None |
| register | Local | Block execution | CPU Register | None |
| static | Local/Global | Entire program | Data Segment | Internal/None |
| extern | Global | Entire program | Data Segment | External |
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.
