Nearby lessons
109 of 124C - Storage Classes
A storage class tells the compiler where a variable lives, how long it lives, and who can see it. Learn the four keywords — auto, register, static and extern — and the three properties they control.
The Four Storage Classes
One table covers the whole topic. Everything that follows is detail:
| Keyword | Stored in | Lifetime | Default value | Scope |
|---|---|---|---|---|
auto | Stack | The block | Garbage | The block |
register | Register (a hint) | The block | Garbage | The block |
static | Data segment | Whole program | Zero | Block or file |
extern | Data segment | Whole program | Zero | Every file |
Three Separate Questions
.c file reach it? Mixing these three up is the reason storage classes feel confusing.Scope — Where the Name Is Visible
Scope is decided by where you write the declaration, not by the storage class:
Lifetime — How Long the Memory Exists
An automatic variable is created on entry to its block and destroyed on exit. A static variable is created once, before main runs, and destroyed when the program ends:
Linkage — Reaching Across Files
Linkage decides whether a name in one .c file refers to the same object as a name in another:
| Linkage | Meaning | How to get it |
|---|---|---|
| External | One object shared by all files | A global, or extern |
| Internal | Private to this file | static at file scope |
| None | Not shareable at all | Any local variable |
Default Values Differ
Where Each One Lives in Memory
A running C program divides its memory into four regions. The storage class decides which one your variable goes to:
static Means Two Different Things
This is the one genuine trap in the topic. The keyword changes lifetime inside a function, and linkage at file scope:
Storage Classes on Functions
Functions accept only static and extern. Functions are external by default, so extern on one is redundant:
Choosing One
In practice the decision is quick:
| You want | Use |
|---|---|
| An ordinary local variable | Nothing — auto is the default |
| A value that survives between calls | static inside the function |
| A helper private to this file | static at file scope |
| A variable shared across files | Define it once; extern in a header |
| Speed from a hot loop counter | Nothing — the optimiser beats register |
static often, extern occasionally in headers, and auto and register essentially never. That is not a gap in your knowledge — it is what current practice looks like.Common Mistakes
- Assuming locals start at zero — only static and global variables do.
- Confusing the two meanings of
static— lifetime in a function, linkage at file scope. - Defining a variable in a header — every including file gets its own copy or a duplicate-symbol error; declare it
externand define it in one.c. - Expecting
extern int x = 5;in a header to work — an initialiser makes it a definition. - Confusing scope with lifetime — a static local still exists after the function returns; you simply cannot name it.
- Reaching for
registerfor speed — compilers have ignored it as a hint for decades.
- C has four storage classes: auto, register, static and extern.
- They control three separate things: scope, lifetime and linkage.
- auto is the default for locals and is almost never written.
- static changes lifetime inside a function, and linkage at file scope.
- extern declares something defined in another file.