basics · beginner · ~15 min

Clamp a value

Compose comparisons into a clamp.

Challenge

Implement int clamp(int v, int lo, int hi) returning v constrained to the range [lo, hi].

Starter code

int clamp(int v, int lo, int hi) {
    /* TODO */
    return v;
}

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