basics · beginner · ~15 min

Clamp an exit code

Reduce a value modulo 256 with correct negative handling.

Challenge

A process exit status is only a byte. Wrap any integer into 0-255 the way the shell does.

Task

Implement int byte_exit(int n) that returns n reduced into the range 0-255 by wrapping modulo 256, matching how a shell reports an exit status. This must be correct for negative n too (e.g. -1 wraps to 255). No main — the grader calls it.

Input

A single int n (may be negative or larger than 255).

Output

Returns n mapped into 0-255.

Example

byte_exit(0)     ->   0
byte_exit(200)   ->   200
byte_exit(256)   ->   0
byte_exit(257)   ->   1
byte_exit(-1)    ->   255

Edge cases

  • Negative inputs must wrap into 0-255, not stay negative — C's % follows the sign of the dividend, so a plain n % 256 is not enough.

Input format

A single int n (may be negative or > 255).

Output format

n wrapped into the range 0-255.

Constraints

Negative n must wrap to a value in 0-255.

Starter code

int byte_exit(int n) {
    /* TODO */
    return 0;
}

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