basics · intermediate · ~15 min

Running total

Accumulate persistent state safely.

Challenge

Implement long running_total(int x) that adds x to an internal running sum (starting at 0) and returns the new total, using a static local.

Starter code

long running_total(int x) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.