Call the same function three times and it prints three different numbers — from a variable that’s initialized to zero right there in the function body. This question from our C Programming Quiz App is the classic test of the static keyword’s second job: giving a local variable a memory that outlives the call.
The Quiz Question
void f(void) {
static int c = 0;
c++;
printf("%d", c);
}
What is printed if f() is called three times in a row?
- 000
- 111
- 123
- 333
The Correct Answer: 123
A static local variable has static storage duration: it is created and initialized once, before the program starts, and it survives between calls. The = 0 is not an assignment that re-runs on every call — it’s a one-time initialization. So c goes 0→1 on the first call, 1→2 on the second, 2→3 on the third, printing 123. Verified on gcc 13.3 and Apple clang 21, clean under -ansi -Wall -Wextra:
$ gcc -ansi -Wall -Wextra static.c && ./a.out 123
Why Each Wrong Answer Is Wrong
Why not 111?
111 is what the non-static version prints — and it’s the answer for readers who missed the keyword. Delete static and c becomes an ordinary automatic variable: born at 0 on every call, incremented to 1, destroyed on return. We ran that version too:
void f(void) {
int c = 0; /* automatic: re-created every call */
c++;
printf("%d", c);
}
$ ./a.out 111
One keyword, completely different lifetime. That’s the whole question.
Why not 000?
000 would require the increment to never happen — but c++ runs on every call, after the (one-time) initialization. Even the non-static version manages to print 1s, not 0s.
Why not 333?
333 imagines all three increments happening before any printing — but each call prints immediately after its own increment. The sequence is strictly interleaved: increment, print, return, repeat.
What static Really Does to a Local
A static local keeps the scope of a local (only code inside f can name c) but takes the lifetime of a global (it lives in the data segment, not on the stack). Two practical consequences:
- The initializer must be a constant expression (in C90/C99), and it runs conceptually at program load — zero cost per call. An uninitialized static starts at 0 automatically, unlike an automatic local, which starts as garbage.
- State persists whether you want it or not. Counters, caches, and one-time-setup flags are the legitimate uses. The dark side: a function with static state is no longer reentrant — recursive calls and threads share the single copy, which is why
strtokis infamous.
Note that static on a file-scope declaration means something entirely different (internal linkage — hiding the name from other translation units). Same keyword, two unrelated jobs; this question is about the local one.
Frequently Asked Questions
Does a static local variable reset on every call?
No. It is initialized exactly once, before the program runs, and keeps its value across calls. That’s why the counter here prints 1, 2, 3 rather than 1, 1, 1.
Where is a static local variable stored?
In the program’s data segment (or BSS if zero-initialized), not on the stack. It has a local name but a global lifetime.
Is a function with a static local reentrant?
No — every call, from any thread or recursion depth, shares the same single variable. If the function is called concurrently, access to the static needs synchronization, or the design should pass state explicitly instead.
Related Reading
- Pass by Value in C – Why inc(n) Doesn’t Change n
- Recursion in C – Complete Guide
- C Aptitude Questions and Answers
Recommended Books
- The C Programming Language – Kernighan & Ritchie (India) | Amazon.com
- C Programming: A Modern Approach – K.N. King (India) | Amazon.com
This question is #27 in the C Programming Quiz App — 155 questions with explanations covering functions, pointers, memory, and more.
Download on Google Play →