Static Variables
Static Variables
A normal local variable is created when its function is called and destroyed when the function returns. A static local variable is different: it's initialized once and retains its value between calls.
Regular vs Static
void counter() {
int count = 0; // reset every call
count++;
printf("%d\n", count); // always prints 1
}
void sticky_counter() {
static int count = 0; // initialized once
count++;
printf("%d\n", count); // prints 1, 2, 3, ...
}
The static keyword tells the compiler to store the variable in a persistent location (the data segment) rather than on the stack. The initializer runs only on the first call.
Use Cases
Static locals are useful for:
- Counters: tracking how many times a function has been called
- Caching: remembering a previously computed result
- State machines: keeping track of internal state between calls
Like the ship's computer on the Enterprise -- it remembers the stardate of every query you've ever made, even after you walk away from the console.
Static at File Scope
When static is used on a global variable or function, it limits visibility to the current file (internal linkage). This is useful in multi-file projects to avoid naming conflicts, but won't affect our single-file exercises.
Assembly View
Check the Assembly tab after running. Static variables appear in the .data section rather than being allocated on the stack with SUB SP instructions.
Your Task
Write a function int next_id() that returns incrementing IDs starting from 1. The first call returns 1, the second returns 2, and so on. Use a static variable to remember the current count.
Print the result of calling next_id() five times, each on a separate line.