static#

Depending on where it appears, static in C controls either cross-file visibility (linkage) or variable lifetime and memory placement.

1. File-Scope static: Internal Linkage#

Applying static to a global variable or function at file scope gives it internal linkage.

  • Visible only within the current translation unit (.c file); extern declarations in other files cannot link against it.
  • Used to encapsulate module-private state and helper functions.
  • Stored in the static data segment (.data / .bss) for the entire duration of the program, just like ordinary globals.
static int internal_count = 0; // Accessible only within this .c file

static int Helper(int x) {     // Private helper hidden from other translation units
    return x * 2;
}

2. Local static: Static Storage Duration in the Data Segment#

A static variable declared inside a function or block is not destroyed when the function returns; its value persists until the program terminates.

  • Why it lives in the data segment rather than the stack:
    • A function’s stack frame is popped on return and overwritten by subsequent calls, making it impossible to preserve values across calls on the stack.
    • A local static variable has the exact same memory region (.data / .bss) and lifetime as a global variable, with only its lexical scope restricted to the enclosing { ... } block.
  • Initialization timing (C vs. C++):
    • C: Only compile-time constant expressions are allowed (eg. static int x = 10;), while runtime calls like static int x = GetSeed(); fail to compile. Values are placed into the data segment before program startup (or zero-initialized in .bss if omitted).
    • C++: Runtime expressions such as static int x = GetSeed(); are also allowed. In that case, GetSeed() runs once when the function is called and execution first reaches the declaration, and is skipped on all subsequent calls (Functions).
#include <stddef.h>

size_t CountCalls(void) {
    static size_t ctr = 0; // Initialized once in the data segment before startup
    return ++ctr;          // Retains value across calls (1, 2, 3, ...)
}

3. Process Memory Layout: .data and .bss Segments#

.data and .bss are peer-level memory segments alongside the stack and heap in a process’s virtual address space, collectively referred to as the static data segment.

[High Address]
+---------------+
|     stack     |  ↓ grows downward — local variables, parameters
|      ...      |
|      ...      |
|     heap      |  ↑ grows upward — dynamic allocation (malloc / free)
+---------------+
|     .bss      |  ─┐ Static data segment: uninitialized or zeroed global/static
|     .data     |  ─┘ (entire program lifetime): non-zero initialized global/static
+---------------+
|     .text     |  Machine code & read-only constants
+---------------+
[Low Address]
  • .data segment: Stores global and static variables explicitly initialized to a non-zero value (eg. static int count = 10;). The initial values are stored directly inside the on-disk executable.
  • .bss segment: Stores uninitialized or zero-initialized global and static variables (eg. static int buf[1024];, static int ctr = 0;). Only the total byte size is recorded in the binary to save disk space, and the OS zeroes the region when loading the process.