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

exscape picture exscape · Dec 4, 2009 · Viewed 18.9k times · Source

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?

Answer

David Thornley picture David Thornley · Dec 4, 2009

First, this isn't really a C thing. You're looking at a function that is written in C for some purpose. The same conventions could be used in any language.

Back in my old Unix days, there was something of a convention that 0 meant success, a positive number meant minor problems, and a negative number meant some sort of failure. Therefore, there was also a sort of convention of if (foo() >= 0) { /* success of a sort */ }.

This was doubtless related to Unix process return codes, where 0 was success.