Definition
A Storage Class in C defines where a variable is stored, its lifetime, its scope (visibility), and its default initial value.
There are 4 types of Storage Classes in C:
- Auto Storage Class (
auto) - Register Storage Class (
register) - Static Storage Class (
static) - External Storage Class (
extern)
1. Auto Storage Class (auto)
Definition
The auto storage class is the default storage class for all local variables declared inside a function.
If you do not write auto, the compiler automatically treats the variable as an auto variable.
Characteristics
- Scope: Local (inside a function)
- Lifetime: Exists only while the function is executing
- Default Value: Garbage value (uninitialized)
- Memory Location: RAM (Stack)
Example
#include<stdio.h>
void demo()
{
auto int x = 10;
printf("%d\n", x);
}
int main()
{
demo();
return 0;
}
Output
10
Another Example
#include<stdio.h>
void test()
{
auto int a;
printf("%d\n", a);
}
int main()
{
test();
return 0;
}
Output
Garbage value
(Actual output may be any random number.)
Advantages
- Fast memory allocation
- Automatically removed after function execution
- Saves memory
Disadvantages
- Cannot retain value between function calls
- Accessible only inside the function
2. Register Storage Class (register)
Definition
The register storage class tells the compiler to store the variable in a CPU register instead of RAM for faster access.
It is mainly used for variables that are accessed frequently, such as loop counters.
Note: The compiler may ignore this request if no register is available.
Characteristics
- Scope: Local
- Lifetime: Till function execution
- Default Value: Garbage value
- Memory Location: CPU Register (if available)
Example
#include<stdio.h>
int main()
{
register int i;
for(i=1; i<=5; i++)
{
printf("%d ", i);
}
return 0;
}
Output
1 2 3 4 5
Important Point
You cannot find the address of a register variable.
register int x = 10;
printf("%p", &x);
This gives an error because register variables may not have a memory address.
Advantages
- Faster execution
- Useful for loop variables
- Improves performance in some cases
Disadvantages
- Compiler may ignore the request
- Address cannot be obtained using
&
3. Static Storage Class (static)
Definition
A static variable keeps its value even after the function ends.
It is created only once and remains in memory throughout the program execution.
Characteristics
- Scope: Local (if declared inside a function)
- Lifetime: Entire program
- Default Value: 0
- Memory Location: Data Segment
Example
#include<stdio.h>
void counter()
{
static int count = 0;
count++;
printf("%d\n", count);
}
int main()
{
counter();
counter();
counter();
return 0;
}
Output
1
2
3
Explanation
First Call
count = 0
count++
count = 1
Output
1
Second Call
The variable is not destroyed.
count = 1
count++
count = 2
Output
2
Third Call
count = 2
count++
count = 3
Output
3
Comparison with Auto Variable
#include<stdio.h>
void demo()
{
int x = 0;
x++;
printf("%d\n", x);
}
int main()
{
demo();
demo();
demo();
return 0;
}
Output
1
1
1
Because x is created every time the function is called.
Advantages
- Retains value between function calls
- Memory allocated only once
- Useful for counters and function state
Disadvantages
- Occupies memory for the entire program
- Incorrect use can make code harder to understand
4. External Storage Class (extern)
Definition
The extern storage class is used to declare a global variable that is defined elsewhere (usually in another file or earlier in the same program).
It allows multiple functions or source files to share the same global variable.
Characteristics
- Scope: Global
- Lifetime: Entire program
- Default Value: 0
- Memory Location: Data Segment
Example (Single File)
#include<stdio.h>
int num = 100;
void display()
{
extern int num;
printf("%d\n", num);
}
int main()
{
display();
return 0;
}
Output
100
Example (Two Files)
File1.c
#include<stdio.h>
int number = 50;
File2.c
#include<stdio.h>
extern int number;
int main()
{
printf("%d", number);
return 0;
}
Output
50
Advantages
- Share global variables across multiple files
- Reduces duplicate variable declarations
- Useful in large projects
Disadvantages
- Excessive use of global variables reduces code maintainability
- Can make debugging more difficult if many files modify the same variable
Comparison Table
| Storage Class | Keyword | Scope | Lifetime | Default Value | Memory Location |
|---|---|---|---|---|---|
| Auto | auto | Local | During function execution | Garbage value | Stack |
| Register | register | Local | During function execution | Garbage value | CPU Register (if available) |
| Static | static | Local/Global | Entire program | 0 | Data Segment |
| Extern | extern | Global | Entire program | 0 | Data Segment |
Key Differences
| Feature | Auto | Register | Static | Extern |
|---|---|---|---|---|
| Scope | Local | Local | Local/Global | Global |
| Lifetime | Function execution | Function execution | Entire program | Entire program |
| Value retained after function call | No | No | Yes | Yes |
| Default value | Garbage | Garbage | 0 | 0 |
| Can access outside function | No | No | Only if declared globally | Yes |
Can take address using & | Yes | No | Yes | Yes |
Summary
auto: Default local variable. Created when the function starts and destroyed when it ends.register: Requests storage in a CPU register for faster access. Mainly used for loop counters.static: Retains its value between function calls and exists for the entire program.extern: Refers to a global variable defined elsewhere, allowing it to be shared across functions or source files.
