C Programming • C17 baseline, standards-specific features labeled
Scope, Lifetime, and Storage Duration
Distinguish where a name is visible from how long the object it names exists.
Block scope limits a name to a region of code. An automatic local object normally exists only during its block execution. A static local object retains its value between calls, while file-scope static limits a name to the current source file.
Example
unsigned next_sequence(void) {
static unsigned sequence = 0;
return ++sequence;
}
What to expect
Calls return 1, then 2, then 3 because the static local keeps its value between calls.
Key idea: Use persistent local state sparingly; it can make a function harder to reason about and test.