c / intermediate
Snippet
Static Variables in Functions
A static local variable retains its value between function calls, initialized only once when the program starts.
snippet.c
1
2
3
4
5
void track_calls() {static int count = 0;count++;printf("Called %d times\n", count);}
Breakdown
1
static int count = 0;
Declares a static variable that persists in memory for the duration of the program.
2
count++;
Increments the value, which will be remembered for the next time the function is called.