Search code examples
clibrarieserror-handling

errno vs errors returned by library functions


For errno, there are a bunch of library functions, like strerror(), etc. to convert or print the error code.

But what about the error codes returned by library functions?

E.g. write() will return EAGAIN, EBADF, etc.

Where do these symbols come from? Are they compatible with strerror() &co?


Solution

  • EAGAIN, EBADF etc. are symbols for system error numbers, defined in errno.h. They are compatible with strerror and related functions.

    Note that write doesn’t return one of these values on error; on error, it returns -1, and sets errno appropriately.

    Few functions return error numbers; amusingly enough, one of them is strerror_r, which returns 0 on success, or EINVAL or ERANGE on failure.