Storage Classes in C
Storage Classes in C
Storage classes define where a variable is stored, how long it lives, and who can access it.
auto
- Storage: Stack
- Scope: Block (inside
{}) - Lifetime: Only during the function call
- Note: This is the default for all local variables — you almost never write
autoexplicitly
register
- Storage: CPU Register (if available, otherwise falls back to stack)
- Scope: Block
- Lifetime: Function call
- Note: A hint to the compiler to optimize for speed. Modern compilers largely ignore this since they optimize register usage themselves
static
- Storage: Static Memory (data segment)
- Scope: Block or File (depending on where it’s declared)
- Lifetime: Entire program duration
- Note: Retains its value between function calls — unlike
auto, it doesn’t reset. When declared at file level, it’s invisible to other files (internal linkage)
extern
- Storage: Static Memory
- Scope: Global (across files)
- Lifetime: Entire program
- Note: Declared elsewhere — tells the compiler “this variable exists in another translation unit.” Used to share variables across
.cfiles
Quick Mental Model
auto/register → born when function starts, die when it ends
static → born at program start, live forever, remember their value
extern → live in another file, borrowed here