C Reference ยท Functions and Scope
Static local variables in C
Reference for static local variables in C: initialization once, values kept between calls, constant initializers, thread safety and pitfalls in recursion.
A local variable declared with static keeps its value between calls. It has block scope, so only the function can name it, but its storage lasts for the whole program. It is created and initialized before main starts, not each time the function runs.
Use static locals for call counters, caches, lookup tables that you do not want to rebuild, and one-time initialization flags. They are a controlled form of global state, with the same drawbacks: hidden dependencies, trouble with recursion and threads, and harder testing.
int next_id(void) { static int counter = 0; /* initialized once */ return ++counter; }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
static type name = constant-expression; /* inside a function body */ static type name; /* implicitly zero-initialized */
| Name | Description |
|---|---|
| scope | Block scope: visible only in the enclosing block. |
| lifetime | Static storage duration: from program start to program end. |
| initializer | Must be a constant expression (a literal, an address constant, or in C23 a constexpr object). Without an initializer the object starts as zero or null. |
| linkage | None. The name cannot be accessed from other functions or files, but you can return a pointer to it. |
Errors / undefined behavior: static int n = rand(); does not compile, because the initializer is not constant. Pointers to a static local stay valid for the whole program, so returning them is safe (unlike automatic variables). Concurrent writes from several threads without synchronization are a data race, which is undefined.
Usage notes
- Initialized once, not on every call. Assignments in the body run every time; the initializer does not. If you need per-call setup, assign inside the body.
- Zero by default. Static objects without an initializer are zero-initialized (numbers 0, pointers null, struct members recursively), unlike automatic variables, which are indeterminate.
- Not thread-safe. C does not lock static locals. Use
_Thread_local(C23:thread_local) for one copy per thread, or a mutex for shared state. Unlike C++, C has no "magic static" guarantee for initialization, but a constant initializer needs no run-time code. - Recursion shares it. Every level of a recursive function sees the same static variable, which can be handy (a depth counter) or a bug.
- Static buffers returned to callers (as in
label) are overwritten by later calls. This is whystrtokandasctimeare awkward; prefer caller-supplied buffers. - Testing and reuse. A function with hidden state behaves differently on each call. For libraries, pass state in a struct (
struct parser *) so callers can have several independent instances. - Not the same as file-scope
static. At file scope,staticmeans internal linkage. See Linkage and static. - Memory is counted in the program image (
.dataor.bss), not on the stack, so large tables as static locals do not risk stack overflow.