basics · intermediate · ~15 min

A counter with static state

Use static storage duration for state that persists across calls.

Challenge

Implement int counter_next(void) that returns 1 on its first call, 2 on the next, and so on, using a static local variable to remember state between calls.

Starter code

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.