Module 4 · Functions, Recursion and Storage Classes — topic 3 of 3
Storage Classes and Scope
auto, register, static and extern decide where a variable lives, how long it lasts and which parts of a program can see it. Learn block scope, file scope, lifetime and linkage.
Learning objectives
- Distinguish scope, lifetime and linkage
- Use static locals to keep state between calls
- Share globals across files with extern and hide them with static
Definition
A storage-class specifier (auto,register,static,extern,_Thread_local) determines an identifier's storage duration (automatic, static or thread), and, together with where it is declared, its linkage (none, internal or external).
Introduction
Every variable has three properties beyond its type. Scope: where in the source its name can be used. Lifetime: when it exists in memory. Linkage: whether the same name in another file refers to the same object.
Storage-class keywords change these properties. static in particular means two different things depending on where you use it — a frequent interview question.
Why it matters
Understanding lifetime explains bugs such as returning the address of a local variable. Understanding linkage is essential for multi-file programs and libraries.
Static locals are a clean way to count calls, cache results or generate IDs without global variables.
Detailed explanation
Block scope: a variable declared inside { } is visible only inside that block. File scope: a variable declared outside all functions is visible from the declaration to the end of the file.
`auto` (the default for locals): automatic storage — created when the block is entered, destroyed when it exits, not initialised automatically. Writing auto explicitly is legal but redundant in C (in C23, auto can also mean type inference).
`register`: a hint that the variable is used heavily. You cannot take its address. Modern compilers ignore the hint for optimisation purposes.
`static` on a local variable: static storage duration — created once, lives for the whole program, keeps its value between calls, and is zero-initialised if not initialised explicitly. Scope stays the block.
`static` on a global variable or function: internal linkage — the name is private to its source file. `extern`: declares a variable defined elsewhere (usually another file) without allocating it.
Syntax
- A static local's initialiser runs only once.
externdeclarations usually go in header files; the single definition goes in one.cfile.- Inner declarations hide (shadow) outer ones with the same name.
Code examples
A static local counter
101 102 103
Block scope and shadowing
global x = 1 inner x = 3 local x = 2
More examples
Automatic vs static lifetime
automatic=1 persistent=1 automatic=1 persistent=2 automatic=1 persistent=3 total=25 after 2 helper calls
Step-by-step explanation
automaticis created fresh on each call and always starts at 0, so it prints 1 every time.persistentis created once; its value survives between calls and increases to 3.static int helper_callsat file scope cannot be referenced from other.cfiles.static int squareis a private helper — another file could define its ownsquarewithout a clash.
Expected output
Running “Automatic vs static lifetime” prints exactly:
Practical applications
- Libraries mark internal helpers
staticso they don't clash with user code. - Random number generators keep their seed in a static variable.
- Configuration values are shared across files with
externdeclarations in a header.
Best practices
- Minimise globals; prefer passing values as parameters.
- Make every file-level function and variable
staticunless other files need it. - Use static locals for state that belongs to one function.
Common mistakes
Returning the address of a local
Avoid
Prefer
value is destroyed when the function returns, leaving a dangling pointer. A static (or heap-allocated) object outlives the call.
Defining a global in a header
Avoid
Prefer
Including the header in two files creates two definitions and a linker error. Declare with extern in the header and define once.
Important notes
- Static locals make a function non-reentrant: it is unsafe to call from multiple threads at once without synchronisation.
_Thread_local(C11) gives each thread its own copy of a static-duration variable.
Key takeaways
- Scope = where a name is visible; lifetime = when the object exists; linkage = sharing across files.
- Static locals persist between calls and are zero-initialised.
staticat file scope makes a name private;externrefers to a definition elsewhere.
Try it yourself
- 1Write a function that prints “first call” only the first time it is called, using a static flag.
- 2Split a program into
main.candcounter.c, sharing a counter variable withextern.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
6 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is the initial value of a global `int count;` with no initialiser?
- ModerateWhat is printed?
- ModerateWhat does `static` mean on a function defined at file scope?