basics · intermediate · ~15 min

Running total

Accumulate persistent state safely.

Challenge

Maintain a running sum across calls using a static local.

Task

Implement long running_total(int x) that adds x to an internal running sum (which starts at 0) and returns the new total. The sum must persist between calls via a static local. No main — the grader calls it.

Input

A single int x to add to the running total.

Output

Returns the updated running total, as a long.

Example

running_total(5)    ->   5
running_total(10)   ->   15
running_total(-3)   ->   12

Edge cases

  • The internal sum starts at 0 before the first call.
  • Negative inputs subtract from the total.

Rules

  • Keep the sum in a static local so it survives between calls.

Input format

A single int x to add to the running total.

Output format

The updated running total, as a long.

Constraints

Use a static local; the sum starts at 0.

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.