Storage classes in C decide how long a variable lives, where it is visible, and where the compiler puts it. C has four storage-class specifiers you use in everyday code: auto, register, extern and static. Since C11 there is also _Thread_local, and C23 adds constexpr as a storage-class specifier for named constants.
Every object has two properties that storage classes control: its storage duration (automatic, static or thread) and its linkage (none, internal or external). Once you know those two ideas, the four keywords stop being a memorisation exercise.
Three terms come up in every discussion of storage classes in C.
{}) or file scope (outside every function)..c file) or external (shared across the whole program).| Specifier | Default place | Storage duration | Linkage |
|---|---|---|---|
auto | inside a block | automatic | none |
register | inside a block | automatic | none |
static | block or file | static | none (block) or internal (file) |
extern | file (or block) | static | external, usually |
Every variable declared inside a function without another specifier is already auto. It is created when execution enters the block and destroyed when the block ends. Its value is indeterminate until you assign it.
Nobody writes auto in C code today, because it is the default. Note that in C23 and in C++11 and later, auto also means type inference (auto x = 5;), so keep the old meaning in mind only when reading legacy code.
register is a hint that a variable is used so often that it should live in a CPU register. Modern compilers ignore the hint and allocate registers better than you can. The one rule that remains is real: you cannot take the address of a register variable.
That restriction is the only practical effect. Write plain local variables and let the optimiser work.
static has two different meanings, depending on where you use it.
A static variable inside a function is created once, initialised once (to zero if you give no value), and lives for the entire program. The name is still visible only inside the function.
The initialiser of a static variable must be a constant expression. Static locals are shared by every call, so they are not thread-safe and make recursion harder to reason about.
Placed on a global variable or a function, static hides the name from other source files. This is the standard way to make a file-private helper.
Another file can define its own calls without a clash, because each has internal linkage.
extern declares that an object or function is defined somewhere else, usually in another file. A declaration says "this exists"; a definition actually reserves memory. You can declare many times but must define exactly once.
Functions at file scope are extern by default, so extern int add(int, int); and int add(int, int); mean the same. Do not write extern int x = 5; at file scope in a header: that is a definition, and including it in two files breaks the link step.
Objects with static storage duration (globals and static locals) are zero-initialised when you give no initialiser. Automatic objects are not.
Typical implementations keep automatic variables on the stack, and static-duration objects in the data or BSS segments. The standard does not mandate this layout, so rely on the duration rules, not the segments.
_Thread_local (or thread_local from <threads.h>, and a keyword in C23) gives each thread its own copy of a variable with static-like lifetime. It can combine with static or extern.
C23 also lets you write constexpr int size = 16; to declare a compile-time constant object.
Local variables default to auto. Global variables and functions default to static storage duration with external linkage.
Yes. A static function has internal linkage, so only code in the same file can call it. Use it for helpers you do not want to expose.
An extern declaration names an object defined elsewhere and allocates no memory. The definition, without extern and usually with an initialiser, appears in exactly one source file.
Almost never. Compilers decide register allocation themselves, and the keyword only forbids taking the variable's address.
Storage classes in C control lifetime and linkage. Use plain locals (implicitly auto) by default, static inside a function to keep state between calls, static at file scope to make names private, and extern to share a single global defined in one file. Treat register as obsolete, and use _Thread_local when each thread needs its own copy.