What Are Storage Classes?
Storage classes control two properties of a variable:
- Scope — where the variable is visible
- Lifetime — how long the variable exists in memory
The Four Storage Classes
auto (Default)
Every local variable is implicitly
auto. It's created when the block is entered and destroyed when the block exits.c
static
static gives a local variable persistent lifetime — it survives between function calls. At file scope, static limits visibility to the current file.c
extern
Declares a variable that's defined in another file. No memory is allocated — it just tells the compiler "this exists somewhere."
c
register
Suggests the compiler should store the variable in a CPU register for faster access. Modern compilers ignore this hint and optimize automatically.
c
Note: You cannot take the address (&) of aregistervariable.
Summary
| Class | Scope | Lifetime | Default Value |
|---|---|---|---|
auto | Block | Block duration | Garbage |
static (local) | Block | Program duration | 0 |
static (file) | File | Program duration | 0 |
extern | Global | Program duration | Defined elsewhere |
register | Block | Block duration | Garbage |