Search code examples
creturn-valueerrno

Why return a negative errno? (e.g. return -EIO)


Another simple example:

if (wpa_s->mlme.ssid_len == 0)
    return -EINVAL;

Why the unary minus? Is this (usually) done for functions that return >0 on success and <(=)0 on failure, or is there some other reason?


Solution

  • That's basically the reasons. Lots of functions have lots of "good" positive results, so that leaves the negative values for error codes.

    C / POSIX error codes are a bit "historically grown," so there's not much sense in trying to attribute too much rhyme or reason to them.

    Many more modern languages throw exceptions for errors so that they don't have to hijack part of their possible response range for error codes. Of course there are trade-offs either way.