Explain the four storage-class specifiers in C. In particular, what does static mean inside a function compared with at file level?
Show the explanation
A storage class decides a variable's lifetime (how long it exists), its scope (where its name is visible) and its linkage (whether other files can see it).
| Specifier | Where | Lifetime | Default value | Notes |
|---|---|---|---|---|
auto | inside a block | until the block ends | garbage (uninitialised) | The default for local variables, so nobody writes it. (In C++11 auto became type deduction instead.) |
register | inside a block | until the block ends | garbage | A hint to keep the variable in a CPU register. You cannot take its address. Modern compilers ignore the hint. |
static (local) | inside a function | the whole program run | 0 | Initialised once; keeps its value between calls. |
static (file level) | outside functions | whole program | 0 | Internal linkage: visible only in this .c file. |
extern | anywhere | whole program | (defined elsewhere) | Declares a variable that is defined in another file, without creating a new one. |
The two meanings of `static`
int next_id(void) {
static int id = 0; /* created once, survives between calls */
return ++id; /* 1, 2, 3, ... */
}
static int hits; /* private to this file; other files cannot link to it */Inside a function, static changes the lifetime. At file level it changes the linkage. Static functions work the same way: they are private helpers for one file.
extern example: config.c has int debug_level = 1; and main.c has extern int debug_level;. Exactly one file may hold the definition. Two definitions cause a linker error.
Original UniCircle question. Last reviewed 22 Oct 2026.
