</>EmbeddedInterview

What are the storage classes in C?

EasyC Programming

Short answer: Storage-class specifiers decide a variable's lifetime (how long it exists), scope (where its name is visible) and linkage (whether other files can refer to it). C has auto, register, static and extern, plus _Thread_local since C11 (and typedef, which is grammatically a storage class but only creates a type alias).

Specifier Lifetime Default initial value Typical location Linkage
auto (default for locals) the enclosing block indeterminate (garbage) stack or register none
register the enclosing block indeterminate a register if the compiler wants none; you cannot take its address
static (local) whole program zero .data / .bss none
static (file scope) whole program zero .data / .bss internal (this file only)
extern whole program zero (at the definition) defined in another file external
_Thread_local the thread zero per-thread storage internal or external

Key points

  • You almost never write auto in C (in C23 it gained a new meaning: type inference, like C++).
  • register is only a hint; modern compilers ignore it for allocation, but it still forbids &var.
  • Static and global variables are zero-initialised by the start-up code (.bss) or copied from flash (.data). Locals are not initialised: reading one before assignment is undefined behaviour.

Embedded relevance: the difference matters for RAM budgeting. A large local array lives on the stack (risk of stack overflow on a 1 KB task stack), while a static array is placed in .bss and shows up in the map file at link time, so you know about it before the device crashes.

Follow-up: "Where do static local variables live?" In .data or .bss, not on the stack, even though their name is only visible inside the function.