Home / Learn Embedded C / The static Keyword in Embedded C
The static Keyword in Embedded C
One keyword, two different jobs: persistent locals, and file-private globals/functions.
How to think about this
In plain terms
static has two distinct meanings in C: on a local variable, it persists the value between calls (covered earlier); on a global variable or function, it hides that name from every other file in the project.
Why we use it
A driver's internal helper function or internal state variable usually should not be callable/visible from unrelated files — marking it static enforces that boundary and avoids accidental name clashes across a large firmware project.
Under the hood
static at file scope changes a symbol's linkage from "external" (visible project-wide) to "internal" (visible only within that one .c file) — the compiler/linker then simply refuses to let other files reference it.
Real-world use
- Driver API design
- Large multi-file firmware projects
Try it
In a real multi-file project, internal_helper could not be called from a different .c file — only public_api could.
| Where static appears | What it does |
|---|---|
| Inside a function, on a local variable | Persists the variable's value between calls (covered in the C refresher group) |
| At file scope, on a global variable | Hides that variable from every other .c file |
| At file scope, on a function | Hides that function from every other .c file |
Try it: static persisting a local variable between calls
Unlike an ordinary local, this count survives from one call to the next — it is initialized only once, not reset every call.
Quick quiz
1. What does static do to a function declared at file scope (outside any other function)?
File-scope static gives a function (or variable) internal linkage: only code in the same file can see it.