Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A static variable in C is a variable declared with the static storage class specifier, which dictates that the variable possesses static storage duration and, depending on its declaration context, internal linkage. Unlike automatic variables, static variables persist in memory for the entire execution lifecycle of the program rather than being created and destroyed upon entering and exiting a scope block.
static data_type variable_name = constant_expression;

Memory Allocation and Initialization

  • Storage Segment: Static variables are allocated in the data segment of the program’s memory layout, not on the stack. Explicitly initialized static variables reside in the .data segment, while uninitialized (or zero-initialized) static variables are placed in the .bss (Block Started by Symbol) segment.
  • Default Initialization: If not explicitly initialized by the programmer, the C compiler automatically initializes static variables to zero (or NULL for pointers) before the program begins execution.
  • Constant Expression Constraint: In C, the initializer for a static variable must be a constant expression. The language does not permit dynamic initialization of static variables at runtime. Attempting to initialize a static variable with a function call or a non-constant value (e.g., static int x = calculate_value();) will result in a compilation error.
  • Initialization Timing: Initialization occurs exactly once, prior to program execution. Because the initialization value is established at compile-time or load-time, there is no runtime assignment operation executed when the control flow passes the declaration.

Contextual Behavior

The static keyword modifies variable behavior differently based on its lexical placement: inside a block (local) or outside all blocks (global).

1. Local Static Variables (Block Scope)

When declared within a function or block, a static variable retains its value between successive invocations of that block.
  • Scope: Block scope (visible only within the lexical block where it is defined).
  • Lifetime: Static storage duration (persists from program startup to termination).
  • Linkage: No linkage (cannot be referenced by name from outside its block, even within the same file).
void execute_block() {
    // Initialized once at load-time. Memory persists across multiple calls.
    static int static_val = 10; 
    
    // Allocated on the stack. Re-initialized on every call.
    int auto_val = 10;          

    static_val++;
    auto_val++;
}

2. Global Static Variables (File Scope)

When declared outside of any function, the static keyword restricts the visibility of the variable strictly to the translation unit (the .c source file and its included headers) in which it is defined.
  • Scope: File scope (visible from the point of declaration to the end of the file).
  • Lifetime: Static storage duration.
  • Linkage: Internal linkage. The compiler does not export the variable’s symbol to the linker. This prevents other translation units from resolving a reference to this variable.
// translation_unit_A.c
static int internal_state = 50; // Internal linkage

void mutate_state() {
    internal_state++;
}
// translation_unit_B.c
extern int internal_state; 

void access_state() {
    // This usage will result in a linker error (undefined reference) 
    // because internal_state is not exported by translation_unit_A.c.
    // (Note: The extern declaration alone does not cause the error; 
    // the error is triggered when the symbol is actually referenced here).
    int local_copy = internal_state; 
}
Master C with Deep Grasping Methodology!Learn More