What does "static" mean in C?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In C, static does not mean only one thing. Its effect depends on where it is used. At file scope, it changes linkage so a name is private to that translation unit. Inside a function, it changes storage duration so a variable keeps its value between calls.
static At File Scope Means Internal Linkage
When you declare a global variable or function as static at file scope, other source files cannot refer to it by name.
This is a form of file-level encapsulation. It is commonly used for helper functions and internal state that should not leak into the program's external symbol space.
Without static, a file-scope declaration normally has external linkage and may be referenced from other translation units with extern.
static Inside A Function Means Persistent Storage
When static is used on a local variable, the variable is created once and retains its value across function calls.
Calling tick() several times prints increasing numbers because count is not re-created every call.
That is the key difference from an ordinary local variable, which has automatic storage duration and is initialized fresh each time execution enters the block.
static Does Not Mean Immutable
A common misunderstanding is that static makes something constant. It does not. A static variable can still be modified unless it is also declared const.
So static is about linkage or storage duration, not about whether the value may change.
Initialization Rules
Static-storage-duration objects are initialized once, before program execution reaches normal runtime logic. If you do not provide an explicit initializer, they are zero-initialized.
That applies both to file-scope static variables and to function-local static variables.
Why File-Local static Functions Are Useful
Marking helper functions static prevents accidental name collisions across source files.
This is an important habit in larger C codebases because the linker otherwise sees many file-level function names as part of the global symbol space.
Local static Variables Need Care
A local static variable can be convenient for counters, caches, or one-time initialization state. But it also introduces hidden state into a function.
That means the function is no longer purely driven by its arguments. Testing and reentrancy become more complicated because the function remembers previous calls.
Common Pitfalls
The biggest mistake is thinking static has one universal meaning in all contexts. Another is using local static state casually in code that later needs to be thread-safe or reentrant. Developers also sometimes forget that file-scope static hides names from other translation units, which can be surprising when they later try to call the function from another source file. Finally, static does not imply const; mutability and linkage are separate concerns.
Summary
- At file scope,
staticgives internal linkage and hides names from other source files. - Inside a function,
staticgives a local variable persistent storage duration. - '
staticdoes not mean constant or immutable.' - Static-storage-duration objects are initialized once and default to zero if not explicitly initialized.
- The keyword is useful, but its real meaning depends entirely on where it appears.
Related reading
- What does the C compiler do to ensure that different but adjacent memory locations are safe to be used on different threads?
- What does the caret ‘’ mean in C/CLI?
- What does the carries_dependency attribute mean?
- What does the thread_local mean in C11?
- What exactly is stdatomic?
- What happens to a detached thread when main exits?
- What is a C analog of C stdpair?
- What is a first chance exception?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.