c – How to know what the errno means?

c – How to know what the errno means?

You can use strerror() to get a human-readable string for the error number. This is the same string printed by perror() but its useful if youre formatting the error message for something other than standard error output.

For example:

#include <errno.h>
#include <string.h>

/* ... */

if(read(fd, buf, 1)==-1) {
    printf(Oh dear, something went wrong with read()! %sn, strerror(errno));
}

Linux also supports the explicitly-threadsafe variant strerror_r().

Instead of running perror on any error code you get, you can retrieve a complete listing of errno values on your system with the following one-liner:

cpp -dM /usr/include/errno.h | grep define E | sort -n -k 3

c – How to know what the errno means?

On Linux there is also a very neat tool that can tell right away what each error code means. On Ubuntu: apt-get install errno.

Then if for example you want to get the description of error type 2, just type errno 2 in the terminal.

With errno -l you get a list with all errors and their descriptions. Much easier that other methods mentioned by previous posters.

Leave a Reply

Your email address will not be published. Required fields are marked *