Skip to chapter content
navan✳THE BUILD LABC17 / GUIDE
0/24 read
CHAPTER 08 / 21 · DATA AND FUNCTIONS

Scope and storage duration.

Separate where a name is visible from how long its object exists.

BY NAVAN SAMILONG READC17 BASELINETHEORY FIRST
01 / THE MENTAL MODEL

The idea behind the syntax.

Scope answers where a name can be used in source code. Storage duration answers how long an object exists while the program runs. These are related but not identical. A block-scope automatic variable normally exists from entry to its block until that block ends. A file-scope object has static storage duration and exists for the program's lifetime, though its name may be restricted to one translation unit.

The static keyword has different effects depending on context. On a block-scope object it gives static storage duration, so the object retains state between calls. At file scope it gives internal linkage, limiting the name to that translation unit. A header generally declares shared interfaces; a source file defines storage or behavior. Putting an ordinary external object definition in a header can create multiple definitions when several source files include it.

02 / THE RULES

What C guarantees.

A local automatic object is initialized when execution reaches its declaration. A static object without an explicit initializer begins with zero-equivalent values before program startup. That lifetime can be useful for persistent counters or caches, but hidden shared state can also make functions harder to reason about, especially with concurrency. Prefer explicit state passed through an interface when several callers need independent behavior.

Linkage is another axis: it determines whether declarations in different scopes or translation units refer to the same entity. An extern declaration can refer to an externally linked object defined elsewhere. A static function at file scope is private to that source file. These rules explain why a program can have a helper named count in two files without collision while a public API name must be coordinated.

03 / UNDER THE SURFACE

Why the detail matters.

Visibility, linkage, and lifetime answer different questions. A block-scope static object is named only within its block but exists for the whole execution. A file-scope static function has internal linkage so another translation unit can use the same helper name independently. An external declaration may be visible at one point in source yet refer to an object defined in another file. Confusing these axes often produces either linker errors or hidden state that callers did not expect.

Persistent local state can be convenient, but it changes the function's behavior from a simple mapping of arguments to result into a history-dependent operation. That affects tests, reuse, and concurrency. A counter that needs to be independent for two clients is better represented by two explicit state objects than by one static local. The example demonstrates the language mechanism; it does not imply that a static counter is the best design for every counting task.

04 / CODE, EXPLAINED

See the rule in context.

scope-and-storage-duration.c
#include <stdio.h>

static unsigned int calls(void) {
    static unsigned int count = 0;
    return ++count;
}
int main(void) {
    unsigned int first = calls();
    unsigned int second = calls();
    printf("%u, %u\n", first, second);
    return 0;
}
FOLLOW THE EXECUTION

The function name calls is visible only in this translation unit because it has file-scope static linkage. Its local count has static storage duration and begins at zero. The first statement calls it and stores 1; the second calls it and stores 2. printf receives those already computed values and displays 1, 2. Separate statements make the intended order explicit.

05 / THE BOUNDARY

Where the rule stops.

Do not rely on a particular evaluation order for function arguments. When ordering matters, compute results in separate statements before passing them to another call.

06 / THE CONNECTION

Keep this model.

A name's scope is a source-code rule; an object's lifetime is a runtime rule. Pointers make that distinction more consequential because an address remains a value even after the object it once identified has ended.

Finished reading?
Built with curiosity. By Navan Sami. ↗C · Types, memory, and the rules behind the code.