C Storage Classes – Explained with Syntax, Examples & Best Practices
In C, storage classes define the scope, visibility, and lifetime of variables and/or functions. They determine where variables are stored, how long they exist, and who can access them.
Types of Storage Classes in C
Storage Class
Keyword
Scope
Lifetime
Stored In
Automatic
auto
Local
Function block
Stack
External
extern
Global
Entire program
Data segment
Static
static
Local/Global
Entire program
Data segment
Register
register
Local
Function block
CPU registers (if available)
void func()
{
int a = 10; // automatic variable
printf("%d", a);
}
auto Storage Class
Definition:
The default storage class for local variables inside functions.
Syntax:
autoint x = 10;
Example:
#include <stdio.h>
void show() {
auto int a = 5;
printf("Value of a: %d\n", a);
}
int main() {
show();
return 0;
}
extern Storage Class
Definition:
Used to declare a global variable or function in another file.
Syntax:
externint count;
Example:
File1.c
#include <stdio.h>
int count = 100;
File2.c
#include <stdio.h>
extern int count;
int main() {
printf("Count is: %d\n", count);
return 0;
}
static Storage Class
Definition:
Preserves variable value even after the function exits.