Storage Classes
A storage class specifier decides how an object or function is stored and how its name links. The specifiers are auto, register, static, extern, and _Thread_local.
Detailed Explanation
C separates three ideas that other languages fold together: scope (where the name is visible), linkage (whether two declarations name the same entity), and storage duration (how long the object lives). Storage class specifiers are the knobs.
auto is the default for block-scope objects and means automatic duration. Writing it is legal and rare. C23 reused auto for type inference, so new code that wants the old meaning should omit the keyword.
register is a hint that the object will be heavily used. Modern compilers ignore the hint. The remaining effect is that you may not take the address of a register object. Leave it out.
static has two meanings, and both matter. On a block-scope object, static gives it static duration: one instance, initialized once, surviving across calls. On a file-scope object or a function, static gives internal linkage: the name is private to the translation unit. A static function is the C equivalent of a private method.
extern says the definition lives elsewhere. extern int count; at file or block scope is a declaration, not a definition. A function declaration is extern by default, so extern on functions is optional. On an object, it is how a header exports a name that one .c file defines.
_Thread_local (spelled thread_local in C23, or via <threads.h>) gives the object thread storage duration. Each thread has its own instance. Combined with static or extern it also sets linkage. A block-scope _Thread_local must be static or extern.
If you write no specifier at file scope, you get external linkage and static duration. If you write none at block scope, you get no linkage and automatic duration. Tentative definitions — int count; at file scope with no initializer — are a definition if no other definition appears in the translation unit. Put the real definition in one file.
Code Examples
#include <stdio.h>
static int hits; /* internal linkage, static duration */
static int tick(void) /* internal linkage: not exported */
{
static int n = 0; /* one instance, survives across calls */
n++;
hits++;
return n;
}
int main(void)
{
printf("%d %d\n", tick(), tick()); /* prints: 1 2 */
printf("%d\n", hits); /* prints: 2 */
return 0;
}#include <stdio.h>
extern int answer; /* declaration; definition is below */
int answer = 42; /* definition, external linkage */
int main(void)
{
printf("%d\n", answer); /* prints: 42 */
return 0;
}Common Gotchas
`static` on a local is a hidden global
it is not thread-safe, and tests that call the function twice share state. Prefer a parameter or a struct.
`static` on a function and `static` on a local are different features that share a keyword
one is linkage, the other is duration.
A header full of `int counter;` (no `extern`, no `static`) defines the object in every file that includes it
the linker will complain, or worse, merge them. Use `extern` in the header.
`register` no longer makes things faster
it only forbids `&x`. Do not use it in new code.