basics · intermediate · ~15 min

Parse an integer

Turn text into a number — what scanf does under the hood.

Challenge

Instead of reading stdin, parse from a string. Implement int parse_int(const char *s, int *out) that parses an optional leading '-' then decimal digits, stores the value in *out, and returns 1 on success or 0 if s is not a valid integer.

Starter code

int parse_int(const char *s, int *out) {
    /* TODO */
    return 0;
}

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