Storage Classes in C
Storage classes in C define the scope, lifetime, default value, and location of variables in memory.
They decide how long a variable will stay alive and where it can be accessed.
What Are Storage Classes?
Each variable in C belongs to a particular storage class. The storage class determines:
- Where the variable is stored (Memory location)
- Default initial value
- Scope (where the variable can be used)
- Lifetime (how long the variable exists)
- Usage type (speed or global control)
Types of Storage Classes
C provides four major storage classes:
- auto β Local variables (default)
- register β Stored in CPU register for fast access
- static β Lifetime throughout program execution
- extern β Global variable defined in another file
1. auto Storage Class
- Default storage class for local variables
- Created when function begins
- Destroyed when function ends
- Stored in stack memory
Default value: Garbage
2. register Storage Class
- Stored in CPU register instead of RAM
- Very fast access (use for loop counters)
- Cannot use & operator (no memory address)
Used for: performance optimization
3. static Storage Class
- Retains value between multiple function calls
- Lifetime = entire program
- Scope = local or global
- Stored in global/static memory area
Default value: 0
static is used to preserve state (Counter, cache, configuration values, etc.)
4. extern Storage Class
- Declares variable defined in another file
- Used for sharing global data across multiple files
- Does not allocate memoryβonly references
Common use: multi-file projects
Comparison Table
Storage | Scope | Lifetime | Default Value | Memory --------------------------------------------------------------- auto | Local | Function call | Garbage | Stack register | Local | Function call | Garbage | register | Local | Function call | Garbage | CPU Register static | Local/Global | Entire program| 0 | Static/Global Segment extern | Global | Entire program| 0 | Global Segment
Memory Layout Overview
- Stack: auto, register variables
- Global/Static Area: static variables, global variables, extern definitions
- Heap: dynamic allocation (malloc/calloc)
π auto & register β short-lived (function-level)
π static β long-lived (program-level)
π extern β globally accessible
Examples for Each Storage Class
Example 1 β auto Variable
#include <stdio.h>
int main() {
auto int x = 10; // auto is optional
printf("x = %d", x);
return 0;
}
Example 2 β register Variable
#include <stdio.h>
int main() {
register int i;
for(i = 1; i <= 5; i++)
printf("%d ", i);
return 0;
}
Example 3 β static Variable (Value Retains)
#include <stdio.h>
void counter(){
static int c = 1; // retains value
printf("%d ", c);
c++;
}
int main(){
counter();
counter();
counter();
return 0;
}
Example 4 β extern Variable (Used Across Files)
// File1.c int num = 50;
// File2.c
#include <stdio.h>
extern int num;
int main(){
printf("%d", num);
return 0;
}
Real-Life Uses of Storage Classes
- auto: temporary values, loops, local calculations
- register: heavy loops, counters, fast-access variables
- static: login attempts counter, cache, state-maintaining logic
- extern: large applications where multiple files share global data
Storage Classes in C β 15 Practical Examples
1. auto β Default Local Variable
#include <stdio.h>
int main(){
auto int a = 10;
printf("%d", a);
return 0;
}
2. auto β Implicit Auto
#include <stdio.h>
int main(){
int x = 5; // auto by default
printf("%d", x);
return 0;
}
3. auto β Local Block Storage
#include <stdio.h>
int main(){
{
auto int a = 20;
printf("%d", a);
}
return 0;
}
4. static β Preserve Value
#include <stdio.h>
void counter(){
static int c = 0;
c++;
printf("%d ", c);
}
int main(){
counter();
counter();
counter();
return 0;
}
5. static β One Time Initialization
#include <stdio.h>
void show(){
static int x = 100;
printf("%d\n", x);
x++;
}
int main(){
show();
show();
return 0;
}
6. static β Inside Loop
#include <stdio.h>
int main(){
for(int i=0; i<3; i++){
static int num = 0;
num++;
printf("%d ", num);
}
return 0;
}
7. static β Used for Caching
#include <stdio.h>
int cached(){
static int value = 50;
return value;
}
int main(){
printf("%d", cached());
return 0;
}
8. extern β Access Global Variable
#include <stdio.h>
int x = 100;
void show(){
extern int x;
printf("%d", x);
}
int main(){
show();
return 0;
}
9. extern β Modify Global Variable
#include <stdio.h>
int value = 10;
void update(){
extern int value;
value += 5;
}
int main(){
update();
printf("%d", value);
return 0;
}
10. extern β Multi-function Usage
#include <stdio.h>
int g = 25;
void fun1(){
extern int g;
g++;
}
void fun2(){
extern int g;
g += 2;
}
int main(){
fun1();
fun2();
printf("%d", g);
return 0;
}
11. register β Faster Variable Access
#include <stdio.h>
int main(){
register int i;
for(i=1; i<=5; i++){
printf("%d ", i);
}
return 0;
}
12. register β Loop Optimization
#include <stdio.h>
int main(){
register int count = 0;
count++;
printf("%d", count);
return 0;
}
13. register β Cannot Use Address
#include <stdio.h>
int main(){
register int n = 10;
// printf("%p", &n); // not allowed
printf("%d", n);
return 0;
}
14. static Global β File Scope
#include <stdio.h>
static int num = 50;
int main(){
printf("%d", num);
return 0;
}
15. static Local + extern Global Combined
#include <stdio.h>
int g = 10;
void test(){
static int s = 5;
extern int g;
s++;
g++;
printf("%d %d\n", s, g);
}
int main(){
test();
test();
return 0;
}
Important Notes
- static is the most important storage class in C
- extern only declares a variable β does not create memory
- register is a request β compiler may ignore it
- auto is default, so usually not used explicitly
Practice Questions
- What is a storage class? List all types.
- Difference between auto and register variables.
- Why do static variables preserve their value?
- Write a program demonstrating extern storage class.
- Where are static variables stored in memory?
Practice Task
Create 4 programs to demonstrate:
β auto variable
β register loop counter
β static counter that increases each call
β extern global variable shared between two files