TRT
Sign in
easyFoundationsconcept

What are the two different meanings of the static keyword in C, and how does it differ from extern?

Contrast static on a local variable (static storage duration in .data/.bss with block scope) against static on a file-scope symbol (internal linkage).

TRT practice prompt — not a verified question from a named employer.

The Runtime Theory Team2 min read
Learn it firstStorage Classes, Scope, and LinkageMaster variable lifecycles across virtual memory, differentiate internal from external linkage, and organize multi-file C architectures.

A strong answer

In C, every identifier has two independent properties: storage duration (how long its memory exists at runtime) and linkage (whether its name is visible to the linker across multiple .c translation units). The static keyword changes different properties depending on where it is written:

  1. Inside a function (static int count = 0;): It changes storage duration from automatic (stack) to static. Instead of being created on the stack on each call and destroyed on return, count is allocated once in the binary's .data or .bss segment before main starts and retains its value across all calls to that function, while keeping block scope (no other function can access count by name).
  2. At file scope (static int helper(void) or static int config;): File-scope variables already have static storage duration; here static changes linkage from external to internal. The symbol is kept private to that single .c translation unit so the linker will not export it, preventing name collisions with other .c files and allowing the compiler to aggressively inline or dead-code-eliminate it.

By contrast, extern declares that a variable or function has external linkage and its storage is defined in another translation unit (e.g., extern int global_errno; in a header tells the compiler the type of global_errno without allocating duplicate storage for it).

What the interviewer is listening for

  • Clearly separates scope (where in source text a name can be referenced), storage duration (lifetime in memory: stack vs .data/.bss), and linkage (internal vs external visibility to ld).
  • Explains both uses of static (persistent local state vs private file-scope module encapsulation).
  • Mentions that uninitialized static and global variables live in .bss and are guaranteed by the C standard to be zero-initialized before main runs.

Common mistakes

  • Believing a local static variable is re-initialized every time the function is called (its initializer is a compile-time constant placed in .data/.bss once at program startup).
  • Using a local static variable inside a function called by multiple threads without synchronization, creating a data race on the shared .data/.bss slot.
  • Omitting static on private helper functions in .c files, polluting the global linker namespace.

This answer walks

Practice follow-ups

  1. 01Where in the ELF binary do initialized vs uninitialized static variables live?
  2. 02Why should helper functions in a .c file that aren't part of its public header be marked static?

Not started

Sign in to save your learning progress.

Sign in to save