basics · intermediate · ~15 min
Use static storage duration for state that persists across calls.
Build a function that remembers how many times it has been called, using a static local.
Implement int counter_next(void) that returns 1 on its first call, 2 on the next, 3 after that, and so on. Keep the count in a static local variable so it persists across calls. No main — the grader calls it.
None (takes no arguments).
Returns the next counter value: 1, then 2, then 3, ... on successive calls.
counter_next() -> 1 (first call)
counter_next() -> 2
counter_next() -> 3
static local to retain state between calls.None (no arguments).
The next counter value (1, 2, 3, ...) on each call.
Use a static local; the first call returns 1.
int counter_next(void) {
/* TODO: use a static local */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.