ERRNO(3) Library functions ERRNO(3)

errno - number of last error

#include <errno.h>

The <errno.h> header file defines the integer variable errno, which is set by system calls and some library functions in the event of an error to indicate what went wrong. Its value is significant only when the call returned an error (usually -1), and a function that does succeed is allowed to change errno.

Sometimes, when -1 is also a valid successful return value one has to zero errno before the call in order to detect possible errors.

errno is defined by the ISO C standard to be a modifiable lvalue of type int, and must not be explicitly declared; errno may be a macro. errno is thread-local; setting it in one thread does not affect its value in any other thread.

Valid error numbers are all non-zero; errno is never set to zero by any library function. All the error names specified by POSIX.1 must have distinct values, with the exception of EAGAIN and EWOULDBLOCK, which may be the same.

POSIX.1 (2001 edition) lists the following symbolic error names. Of these, EDOM and ERANGE are in the ISO C standard. ISO C Amendment 1 defines the additional error number EILSEQ for coding errors in multibyte or wide characters.

Arg list too long
Permission denied
Address in use
Address not available
Address family not supported
Resource temporarily unavailable
Connection already in progress
Bad file descriptor
Bad message
Resource busy
Operation canceled
No child processes
Connection aborted
Connection refused
Connection reset
Resource deadlock avoided
Destination address required
Domain error
Reserved
File exists
Bad address
File too large
Host is unreachable
Identifier removed
Illegal byte sequence
Operation in progress
Interrupted function call
Invalid argument
Input/output error
Socket is connected
Is a directory
Too many levels of symbolic links
Too many open files
Too many links
Inappropriate message buffer length
Reserved
Filename too long
Network is down
Connection aborted by network
Network unreachable
Too many open files in system
No buffer space available
No message is available on the STREAM head read queue
No such device
No such file or directory
Exec format error
No locks available
Reserved
Not enough space
No message of the desired type
Protocol not available
No space left on device
No STREAM resources
Not a STREAM
Function not implemented
The socket is not connected
Not a directory
Directory not empty
Not a socket
Not supported
Inappropriate I/O control operation
No such device or address
Operation not supported on socket
Value too large to be stored in data type
Operation not permitted
Broken pipe
Protocol error
Protocol not supported
Protocol wrong type for socket
Result too large
Read-only file system
Invalid seek
No such process
Stale file handle
STREAM ioctl() timeout
Operation timed out
Text file busy
Operation would block (may be same value as EAGAIN)
Improper link

A common mistake is to do


if (somecall() == -1) {

    printf("somecall() failed\n");

    if (errno == ...) { ... }
}
where errno no longer needs to have the value it had upon return from somecall() (i.e., it may have been changed by the printf()). If the value of errno should be preserved across a library call, it must be saved:

if (somecall() == -1) {

    int errsv = errno;

    printf("somecall() failed\n");

    if (errsv == ...) { ... }
}

It was common in traditional C to declare errno manually (i.e., extern int errno) instead of including <errno.h>. Do not do this. It will not work with modern versions of the C library. However, on (very) old Unix systems, there may be no <errno.h> and the declaration is needed.

err(3), perror(3), strerror(3)

2004-12-17

Different Versions of this Page: