Skip to content
UniCircle
EasyConceptualStorage classes and scope

Storage classes in C: auto, static, extern and register

Question C-STO-003

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).

SpecifierWhereLifetimeDefault valueNotes
autoinside a blockuntil the block endsgarbage (uninitialised)The default for local variables, so nobody writes it. (In C++11 auto became type deduction instead.)
registerinside a blockuntil the block endsgarbageA hint to keep the variable in a CPU register. You cannot take its address. Modern compilers ignore the hint.
static (local)inside a functionthe whole program run0Initialised once; keeps its value between calls.
static (file level)outside functionswhole program0Internal linkage: visible only in this .c file.
externanywherewhole program(defined elsewhere)Declares a variable that is defined in another file, without creating a new one.

The two meanings of `static`

C
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.