linux-sysprog · intermediate · ~15 min

Set errno in your own function

Follow the errno-setting convention in your APIs.

Challenge

Implement int checked_div(int a, int b, int *out) that, on b == 0, sets errno = EDOM and returns -1; otherwise stores a/b in *out and returns 0.

Starter code

#include <errno.h>

int checked_div(int a, int b, int *out) {
    /* TODO */
    return -1;
}

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