static Variables
Persistent locals.
static Variables is a free C Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The static Keyword
A static local variable keeps its value between calls to the function. Its lifetime is the whole program.
#include <stdio.h>
void f(void) {
static int count = 0;
count++;
printf("count = %d\n", count);
}
int main(void) {
f();
f();
f();
return 0;
}Initialized Once
A static variable is initialized only the first time the function runs. Later calls skip the initializer.
#include <stdio.h>
void f(void) {
static int x = 100;
printf("x = %d\n", x);
x += 10;
}
int main(void) {
f();
f();
return 0;
}Default Zero
Unlike auto variables, a static variable without an initializer starts at 0.
#include <stdio.h>
void f(void) {
static int n;
printf("n = %d\n", n);
n++;
}
int main(void) {
f();
f();
return 0;
}Counting Calls
A classic use is counting how many times a function has been invoked.
#include <stdio.h>
int nextId(void) {
static int id = 0;
return ++id;
}
int main(void) {
printf("%d %d %d\n", nextId(), nextId(), nextId());
return 0;
}Static Lifetime, Local Scope
A static local still has local scope. Only the function can see it, but it persists across calls.
#include <stdio.h>
void tick(void) {
static int t = 0;
t++;
printf("tick %d\n", t);
}
int main(void) {
tick();
tick();
return 0;
}Static vs auto
Compare: an auto counter resets every call, while a static one accumulates.
#include <stdio.h>
void autoCounter(void) {
int c = 0;
c++;
printf("auto: %d\n", c);
}
int main(void) {
autoCounter();
autoCounter();
return 0;
}Static at File Scope
At file scope, static means internal linkage: the variable is private to its source file.
#include <stdio.h>
static int secret = 7;
int main(void) {
printf("secret = %d\n", secret);
return 0;
}Caching With Static
Static variables can cache a computed value so it is calculated only once.
#include <stdio.h>
int getBase(void) {
static int base = 0;
if (base == 0) {
base = 50;
printf("computed once\n");
}
return base;
}
int main(void) {
printf("%d\n", getBase());
printf("%d\n", getBase());
return 0;
}Accumulating State
Static locals let a function remember a running total without a global.
#include <stdio.h>
int addToTotal(int v) {
static int total = 0;
total += v;
return total;
}
int main(void) {
printf("%d\n", addToTotal(10));
printf("%d\n", addToTotal(5));
return 0;
}A Caution
Static state makes a function depend on history, which can be harder to test. Use it deliberately.
#include <stdio.h>
int stepNumber(void) {
static int s = 0;
return ++s;
}
int main(void) {
printf("step %d\n", stepNumber());
printf("step %d\n", stepNumber());
return 0;
}Combining With Loops
A static local inside a loop body keeps its value across both iterations and calls.
#include <stdio.h>
void run(void) {
static int seen = 0;
for (int i = 0; i < 2; i++)
seen++;
printf("seen = %d\n", seen);
}
int main(void) {
run();
run();
return 0;
}Quick Check
Test your understanding of static variables.
Recap: static Variables
You learned about persistent locals.
staticlocals live for the whole program- They initialize once and default to 0
- They keep local scope but persist values
- At file scope
staticgives internal linkage - Great for counters and caches
Frequently asked questions
Is the “static Variables” lesson free?
Yes — the full text of “static Variables” is free to read here on the web, and the C Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO.
What will I learn in “static Variables”?
Persistent locals. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C Academy?
No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “static Variables” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C Academy lesson?
Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- auto and Local Scope
- static Variables
- extern and Global
- register and const