C - Storage Classes in C
In C programming, storage classes define how and where variables are stored, how long they exist in memory, and where they can be accessed from. In simple terms, storage classes control the lifetime, scope, and visibility of variables and functions.
There are four main storage classes in C:
-
auto
-
static
-
extern
-
register
Let us understand each one clearly.
1. auto Storage Class
The auto storage class is the default for all local variables declared inside a function or block.
Example:
void display() {
auto int x = 10;
}
Important points:
-
It is stored in memory (RAM).
-
It exists only while the function is running.
-
It cannot be accessed outside the block where it is declared.
-
Writing
autois optional because all local variables are auto by default.
Lifetime: Until the function finishes execution
Scope: Inside the block or function only
2. static Storage Class
The static storage class changes the lifetime of a variable.
There are two main uses of static:
A. Static local variable
When a variable inside a function is declared as static, it keeps its value even after the function ends.
Example:
void counter() {
static int count = 0;
count++;
printf("%d\n", count);
}
If you call counter() multiple times, the value of count will not reset to 0. It will continue increasing.
Important points:
-
Memory is allocated only once.
-
The value is preserved between function calls.
-
Scope remains local to the function.
Lifetime: Entire program execution
Scope: Inside the function only
B. Static global variable
If a global variable is declared as static, it cannot be accessed from other files.
Example:
static int total = 100;
This is useful in multi-file programs to restrict access.
3. extern Storage Class
The extern keyword is used to declare a global variable that is defined in another file.
Example:
File1.c:
int number = 50;
File2.c:
extern int number;
Important points:
-
It does not create a new variable.
-
It tells the compiler that the variable exists somewhere else.
-
Mostly used in multi-file programs.
Lifetime: Entire program execution
Scope: Global
4. register Storage Class
The register storage class suggests that the variable should be stored in a CPU register instead of RAM.
Example:
void sum() {
register int i;
}
Important points:
-
Used for frequently accessed variables.
-
Access is faster than normal variables.
-
You cannot use the address operator
&on register variables. -
The compiler may ignore the request if registers are not available.
Lifetime: Until function execution ends
Scope: Inside the block
Summary Table
| Storage Class | Lifetime | Scope | Special Feature |
|---|---|---|---|
| auto | Function execution | Local | Default for local variables |
| static | Entire program | Local or file level | Value preserved |
| extern | Entire program | Global | Used across multiple files |
| register | Function execution | Local | Stored in CPU register (if possible) |
Why Storage Classes Are Important
Storage classes help you:
-
Control memory usage
-
Manage variable access in large programs
-
Protect data from being modified accidentally
-
Build multi-file projects properly
Understanding storage classes is very important when working on real-world C applications, especially system-level and embedded programming.