Skip to content

Static

In C, the static keyword modifies two fundamental properties of variables and functions:

  1. Lifetime: How long the variable stays in memory.

  2. Visibility (Linkage): Who can see and use the variable/function.

The behavior depends on where you place the keyword.


1. Static Local Variables (Inside a Function)

Purpose: To create a variable that retains its value between function calls.

Mechanism

  • Storage: Unlike normal local variables (which are stored on the Stack and destroyed when the function exits), static local variables are stored in the Global Data Segment (specifically .data or .bss sections).

  • Lifetime: They exist for the entire duration of the program execution.

  • Initialization: They are initialized only once when the program starts (before main runs). If you enter the function a second time, the initialization line is skipped.

Code Example

#include <stdio.h>

void counter() {
    // This line is executed only ONCE at program startup.
    // It is stored in the Data Segment, not the Stack.
    static int count = 0; 

    count++;
    printf("Count is: %d\n", count);
}

int main() {
    counter(); // Output: Count is: 1
    counter(); // Output: Count is: 2 (It remembers the previous value)
    counter(); // Output: Count is: 3
    return 0;
}

2. Static Global Variables (File Scope)

Purpose: To limit the scope of a global variable to the current file (Private Global).

Mechanism

  • Linkage: Standard global variables have External Linkage (visible to the Linker). static global variables have Internal Linkage.

  • Visibility: The variable is hidden from other translation units (.c files). If file1.c has static int x;, file2.c cannot access x using extern.

  • Encapsulation: This is the C way of creating "private" members. It prevents naming conflicts with variables in other files.

Code Example

File: module.c

// Only functions inside module.c can see this variable.
// If another file defines 'int secret', there will be no conflict.
static int secret = 42; 

int get_secret() {
    return secret;
}

File: main.c

extern int secret; // ERROR: Linker cannot find 'secret'

3. Static Functions

Purpose: To limit the visibility of a function to the file where it is defined.

Mechanism

  • Just like static global variables, static functions have Internal Linkage.

  • The function name is not exported to the symbol table for the Linker.

  • Benefits:

    1. Safety: Prevents other files from accidentally calling helper functions intended for internal use only.

    2. Optimization: Since the compiler knows the function cannot be called from outside, it might inline the function code more aggressively.

Code Example

// Internal helper function. 
// Can be named 'init' without clashing with 'init' in other libraries.
static void internal_helper() {
    printf("Doing internal work...\n");
}

// Public function
void public_api() {
    internal_helper(); // OK: Called from same file
}

4. Under the Hood: Memory Layout

To understand static fully, you must look at how C programs use memory segments.

A. The Stack vs. The Data Segment

  • auto variables (Standard Local): Pushed onto the Stack. When the function returns, the stack frame is popped, and the memory is reclaimed/overwritten.

  • static variables: Stored in fixed memory addresses in the Data Segment. The address remains valid and the content remains untouched until the OS terminates the program.

B. The Zero-Initialization Rule (.bss Section)

If you declare a static variable but do not assign a value, it is guaranteed to be Zero:

static int x; // x is guaranteed to be 0
  • Why? Uninitialized static variables go into the .bss segment (Block Started by Symbol).

  • Startup: Before main() runs, the C Runtime (CRT) startup code iterates over the .bss memory block and sets every byte to zero. Standard stack variables do not get this treatment (they contain garbage values).


Summary Table

Concept Declaration Location Storage Area Lifetime Scope (Visibility) Linkage
Local Variable Inside Function Stack Function Block Inside Function None
Static Local Inside Function Data / BSS Entire Program Inside Function None
Global Variable Outside Function Data / BSS Entire Program Entire Project External
Static Global Outside Function Data / BSS Entire Program Current File Only Internal

Key Takeaways

  1. Inside a function: static means "Keep my value alive" (Duration).

  2. Outside a function: static means "Keep me private" (Visibility).

  3. Default Value: Static variables are always initialized to 0 by default.