- What a
staticvariable is, in plain words - Why a
staticvariable keeps its value when the function ends - How to count how many times a function has been called
- Why a normal (auto) variable "forgets" its value and a
staticone doesn't - Why
staticis safer than using a global variable for this
What Does static Mean?
Normally, when a function finishes running, every variable inside it is thrown away. The next time you call the function, the variables start fresh. That's how auto variables work.
A static variable is different. It is created once, stays alive for the whole program, and remembers its value each time the function is called again. From outside the function, you can't see it — but it's still there.
Think of it like a small notebook inside a function. Everyone who calls the function can read and write in the notebook, but only the function itself is allowed to open it. The notebook survives between visits.
💡 In one line: A static local variable keeps its value between calls — but you can only use it from inside the function that declares it.
Simple Rules to Remember
| Question | Answer |
|---|---|
| Created when? | Once, before the program starts |
| Destroyed when? | When the program ends |
| Starting value? | Zero, if you don't give it one |
| Who can see it? | Only the function that declares it |
| Does it remember values? | Yes — that's the whole point |
Complete C Program
Program: A counter that remembers how many times it was called
#include <stdio.h>
/* This function remembers its count between calls */
void staticCounter(void) {
static int count = 0; /* created only once */
count++;
printf("staticCounter called %d time(s)\n", count);
}
/* This function forgets its count every time */
void autoCounter(void) {
int count = 0; /* reset to 0 on every call */
count++;
printf("autoCounter called %d time(s)\n", count);
}
int main(void) {
int i;
printf("--- using static ---\n");
for (i = 0; i < 5; i++)
staticCounter();
printf("\n--- using auto ---\n");
for (i = 0; i < 3; i++)
autoCounter();
return 0;
}
Program Output
--- using static ---
staticCounter called 1 time(s)
staticCounter called 2 time(s)
staticCounter called 3 time(s)
staticCounter called 4 time(s)
staticCounter called 5 time(s)
--- using auto ---
autoCounter called 1 time(s)
autoCounter called 1 time(s)
autoCounter called 1 time(s)
Step-by-Step Explanation
Let's walk through the program slowly and see exactly what happens.
1. The function with static:
void staticCounter(void) {
static int count = 0;
count++;
printf("staticCounter called %d time(s)\n", count);
}
The first time staticCounter() runs, count is created and set to 0. It goes up to 1 and prints 1. When the function ends, count does not disappear. The second time the function runs, the line count = 0 is skipped — count is already there with the value 1. It goes up to 2 and prints 2. This continues: 3, 4, 5.
2. The function without static:
void autoCounter(void) {
int count = 0;
count++;
printf("autoCounter called %d time(s)\n", count);
}
Here count is an ordinary variable. Every call starts it at 0, adds 1, prints 1. When the function ends, count is gone. The next call starts all over again. That's why it always prints 1.
3. Calling both from main:
for (i = 0; i < 5; i++) staticCounter();
for (i = 0; i < 3; i++) autoCounter();
The first loop calls the static version five times — you see 1, 2, 3, 4, 5. The second loop calls the auto version three times — you see 1, 1, 1. Same code, different storage class — completely different behaviour.
4. Why is static better than a global here?
A global variable could do the same job, but it would be visible to every function in the file — someone else might accidentally reset it. With static, the counter belongs only to staticCounter(). Other code can't touch it. You get persistence without giving up safety.
🎯 Remember this: static gives a function a memory of its own. The value survives between calls, but nobody outside the function can see the name. If you want a counter, a cache, or a "has this already been done?" flag inside a function — static is your tool.
Frequently Asked Questions
1. When does a static variable get its starting value?
Before the program runs — not the first time the function is called. The compiler puts the initial value into the program's data and it's ready to use the moment main starts. That's why the initializer is skipped on later calls.
2. What if I don't give a static variable a starting value?
It starts at zero. The C standard guarantees this for static variables. Ordinary auto variables are different — they contain garbage until you assign a value. So static int count; is safe, but int count; is not.
3. Can I use a static variable from another function?
No. A static local can only be seen inside the function that declares it. Its name is not exported. Even if you tried to write printf("%d", count); in main, the compiler would refuse — count is not visible there.
4. Are static variables safe in multi-threaded programs?
No. If two threads call the same function at the same moment, they both read and write the same static variable. One thread might overwrite the other's value. For thread-safe state, use _Thread_local (C11) or protect the variable with a mutex.
5. What's the difference between a static local and a static global?
A static local lives inside a function — only that function can see it. A static global is at the top of the file — every function in the file can see it, but other files cannot. Both live for the whole program. The difference is who can see the name.
💡 Tip: If a function needs to remember something from one call to the next, reach for static first — not a global. It keeps the state private and makes your code easier to reason about.