errno.h

{{Short description|Header file for C programs}}

{{C Standard Library}}

{{lowercase|title=errno.h}}

errno.h is a header file in the standard library of the C programming language. It defines macros for reporting and retrieving error conditions using the symbol errno (short form for "error number").International Standard for Programming Language C (C11), ISO/IEC 9899:2011, p. 205

errno acts like an integer variable. A value (the error number) is stored in errno by certain library functions when they detect errors. At program startup, the value stored is zero. Library functions store only values greater than zero. Any library function can alter the value stored before return, whether or not they detect errors.International Standard for Programming Language C (C99), ISO/IEC 9899:1999, p. 186 Most functions indicate that they detected an error by returning a special value, typically NULL for functions that return pointers, and -1 for functions that return integers. A few functions require the caller to preset errno to zero and test it afterwards to see if an error was detected.

The errno macro expands to an lvalue with type int, sometimes with the extern and/or volatile type specifiers depending upon the platform.{{cite web |url=https://www.gnu.org/software/libc/manual/html_node/Checking-for-Errors.html |title=Checking for Errors |date=2014-02-08 |website=The GNU C Library (glibc) |publisher=GNU Project |access-date=2014-06-25}} Originally this was a static memory location, but macros are almost always used today to allow for multi-threading, so that each thread will see its own thread-local error number.

The header file also defines macros that expand to integer constants that represent the error codes. The C standard library only requires three to be defined:

class="wikitable"
{{tt|EDOM}}A parameter was outside a function's domain, e.g. {{Code|lang=c|sqrt(-1)}}
{{tt|ERANGE}}A result outside a function's range, e.g. {{Code|lang=c|strtol("0xfffffffff", NULL, 0)}} on systems with a 32-bit wide long
{{tt|EILSEQ}}(Required since 1994 Amendment 1 to C89 standard){{cite web|url=http://www.lysator.liu.se/c/na1.html|title=A brief description of Normative Addendum 1|access-date=2013-09-12}}
Illegal byte sequence, e.g. {{Code|lang=c|mbstowcs(buf, "\xff", 1)}} on systems that use UTF-8.

POSIX compliant operating systems like AIX, Linux or Solaris include many other error values, many of which are used much more often than the above ones, such as {{tt|EACCES}} for when a file cannot be opened for reading.{{man|bd|errno.h|SUS|system error numbers}} C++11 additionally defines many of the same values found within the POSIX specification.{{cite web |url=http://en.cppreference.com/w/cpp/error/errno_macros |title=Error numbers - cppreference.com |access-date=2015-05-08 }}

Traditionally, the first page of Unix system manuals, named intro(2), lists all errno.h macros, but this is not the case with Linux, where these macros are instead listed in the errno(3).{{sfn|Stevens|Rago|2013|p=14}}

An {{code|errno}} can be translated to a descriptive string using strerror (defined in string.h) or a BSD extension called {{code|sys_errlist}}. The translation can be printed directly to the standard error stream using perror (defined in stdio.h). As {{code|strerror}} in many Unix-like systems is not thread-safe, a thread-safe version {{code|strerror_r}} is used, but conflicting definitions from POSIX and GNU makes it even less portable than the {{code|sys_errlist}} table.{{cite web |last1=McCabe |first1=Colin |title=The problem with strerror |url=http://www.club.cc.cmu.edu/~cmccabe/blog_strerror.html |website=www.club.cc.cmu.edu}}

POSIX errors

The GNU C library (GLIBC) provides the additional POSIX error values macros in the header file {{Code|errno.h}}.{{Cite web |title=Error Codes (The GNU C Library) |url=https://www.gnu.org/software/libc/manual/html_node/Error-Codes.html |date=Mar 29, 2025| access-date=Apr 5, 2025 |website=www.gnu.org}} These are the descriptions of the macros provided by strerror.

class="wikitable sortable"
SymbolValue{{cite web |author= |title=Debugging: POSIX errors |url=https://docs.particle.io/reference/device-os/api/debugging/posix-errors/ |website=particle.io |access-date=Apr 5, 2025

}}

Description
{{tt|EPERM}}1Operation not permitted
{{tt|ENOENT}}2No such file or directory
{{tt|ESRCH}}3No such process
{{tt|EINTR}}4Interrupted system call
{{tt|EIO}}5Input/output error
{{tt|ENXIO}}6No such device or address
{{tt|E2BIG}}7Argument list too long
{{tt|ENOEXEC}}8Exec format error
{{tt|EBADF}}9Bad file descriptor
{{tt|ECHILD}}10No child processes
{{tt|EAGAIN}}11Resource temporarily unavailable
{{tt|ENOMEM}}12Cannot allocate memory
{{tt|EACCES}}13Permission denied
{{tt|EFAULT}}14Bad address
{{tt|ENOTBLK}}15Block device required
{{tt|EBUSY}}16Device or resource busy
{{tt|EEXIST}}17File exists
{{tt|EXDEV}}18Invalid cross-device link
{{tt|ENODEV}}19No such device
{{tt|ENOTDIR}}20Not a directory
{{tt|EISDIR}}21Is a directory
{{tt|EINVAL}}22Invalid argument
{{tt|ENFILE}}23Too many open files in system
{{tt|EMFILE}}24Too many open files
{{tt|ENOTTY}}25Inappropriate ioctl for device
{{tt|ETXTBSY}}26Text file busy
{{tt|EFBIG}}27File too large
{{tt|ENOSPC}}28No space left on device
{{tt|ESPIPE}}29Illegal seek
{{tt|EROFS}}30Read-only file system
{{tt|EMLINK}}31Too many links
{{tt|EPIPE}}32Broken pipe
{{tt|EDOM}}33Numerical argument out of domain
{{tt|ERANGE}}34Numerical result out of range
{{tt|EDEADLK}}35Resource deadlock avoided
{{tt|ENAMETOOLONG}}36File name too long
{{tt|ENOLCK}}37No locks available
{{tt|ENOSYS}}38Function not implemented
{{tt|ENOTEMPTY}}39Directory not empty
{{tt|ELOOP}}40Too many levels of symbolic links
{{tt|ENOMSG}}42No message of desired type
{{tt|EIDRM}}43Identifier removed
{{tt|ECHRNG}}44Channel number out of range
{{tt|EL2NSYNC}}45Level 2 not synchronized
{{tt|EL3HLT}}46Level 3 halted
{{tt|EL3RST}}47Level 3 reset
{{tt|ELNRNG}}48Link number out of range
{{tt|EUNATCH}}49Protocol driver not attached
{{tt|ENOCSI}}50No CSI structure available
{{tt|EL2HLT}}51Level 2 halted
{{tt|EBADE}}52Invalid exchange
{{tt|EBADR}}53Invalid request descriptor
{{tt|EXFULL}}54Exchange full
{{tt|ENOANO}}55No anode
{{tt|EBADRQC}}56Invalid request code
{{tt|EBADSLT}}57Invalid slot
{{tt|EBFONT}}59Bad font file format
{{tt|ENOSTR}}60Device not a stream
{{tt|ENODATA}}61No data available
{{tt|ETIME}}62Timer expired
{{tt|ENOSR}}63Out of streams resources
{{tt|ENONET}}64Machine is not on the network
{{tt|ENOPKG}}65Package not installed
{{tt|EREMOTE}}66Object is remote
{{tt|ENOLINK}}67Link has been severed
{{tt|EADV}}68Advertise error
{{tt|ESRMNT}}69Srmount error
{{tt|ECOMM}}70Communication error on send
{{tt|EPROTO}}71Protocol error
{{tt|EMULTIHOP}}72Multihop attempted
{{tt|EDOTDOT}}73RFS specific error
{{tt|EBADMSG}}74Bad message
{{tt|EOVERFLOW}}75Value too large for defined data type
{{tt|ENOTUNIQ}}76Name not unique on network
{{tt|EBADFD}}77File descriptor in bad state
{{tt|EREMCHG}}78Remote address changed
{{tt|ELIBACC}}79Can not access a needed shared library
{{tt|ELIBBAD}}80Accessing a corrupted shared library
{{tt|ELIBSCN}}81.lib section in a.out corrupted
{{tt|ELIBMAX}}82Attempting to link in too many shared libraries
{{tt|ELIBEXEC}}83Cannot exec a shared library directly
{{tt|EILSEQ}}84Invalid or incomplete multibyte or wide character
{{tt|ERESTART}}85Interrupted system call should be restarted
{{tt|ESTRPIPE}}86Streams pipe error
{{tt|EUSERS}}87Too many users
{{tt|ENOTSOCK}}88Socket operation on non-socket
{{tt|EDESTADDRREQ}}89Destination address required
{{tt|EMSGSIZE}}90Message too long
{{tt|EPROTOTYPE}}91Protocol wrong type for socket
{{tt|ENOPROTOOPT}}92Protocol not available
{{tt|EPROTONOSUPPORT}}93Protocol not supported
{{tt|ESOCKTNOSUPPORT}}94Socket type not supported
{{tt|EOPNOTSUPP}}95Operation not supported
{{tt|EPFNOSUPPORT}}96Protocol family not supported
{{tt|EAFNOSUPPORT}}97Address family not supported by protocol
{{tt|EADDRINUSE}}98Address already in use
{{tt|EADDRNOTAVAIL}}99Cannot assign requested address
{{tt|ENETDOWN}}100Network is down
{{tt|ENETUNREACH}}101Network is unreachable
{{tt|ENETRESET}}102Network dropped connection on reset
{{tt|ECONNABORTED}}103Software caused connection abort
{{tt|ECONNRESET}}104Connection reset by peer
{{tt|ENOBUFS}}105No buffer space available
{{tt|EISCONN}}106Transport endpoint is already connected
{{tt|ENOTCONN}}107Transport endpoint is not connected
{{tt|ESHUTDOWN}}108Cannot send after transport endpoint shutdown
{{tt|ETOOMANYREFS}}109Too many references: cannot splice
{{tt|ETIMEDOUT}}110Connection timed out
{{tt|ECONNREFUSED}}111Connection refused
{{tt|EHOSTDOWN}}112Host is down
{{tt|EHOSTUNREACH}}113No route to host
{{tt|EALREADY}}114Operation already in progress
{{tt|EINPROGRESS}}115Operation now in progress
{{tt|ESTALE}}116Stale file handle
{{tt|EUCLEAN}}117Structure needs cleaning
{{tt|ENOTNAM}}118Not a XENIX named type file
{{tt|ENAVAIL}}119No XENIX semaphores available
{{tt|EISNAM}}120Is a named type file
{{tt|EREMOTEIO}}121Remote I/O error
{{tt|EDQUOT}}122Disk quota exceeded
{{tt|ENOMEDIUM}}123No medium found
{{tt|EMEDIUMTYPE}}124Wrong medium type
{{tt|ECANCELED}}125Operation canceled
{{tt|ENOKEY}}126Required key not available
{{tt|EKEYEXPIRED}}127Key has expired
{{tt|EKEYREVOKED}}128Key has been revoked
{{tt|EKEYREJECTED}}129Key was rejected by service
{{tt|EOWNERDEAD}}130Owner died
{{tt|ENOTRECOVERABLE}}131State not recoverable
{{tt|ERFKILL}}132Operation not possible due to RF-kill
{{tt|EHWPOISON}}133Memory page has hardware error
{{tt|ENOTSUP}}134Not supported parameter or option
{{tt|ENOMEDIUM}}135Missing media
{{tt|EILSEQ}}138Invalid multibyte sequence
{{tt|EOVERFLOW}}139Value too large
{{tt|ECANCELED}}140Asynchrononous operation stopped before normal completion
{{tt|ENOTRECOVERABLE}}141State not recoverable
{{tt|EOWNERDEAD}}142Previous owner died
{{tt|ESTRPIPE}}143Streams pipe error

The macro names and meanings for error codes are defined in the [https://pubs.opengroup.org/onlinepubs/9799919799/ POSIX Standards definition] however the numeric values are NOT, though by convention the values appear to be the same across different versions of Unix.{{citation needed|date=April 2025}} Programs should not rely on specific numeric values and should test code using the macro names specified in the ERRORS section of the man page of the associated function. For source code readability and portability the use of the standard macro names in code is highly recommended.{{cite web |author= |date=Jan 26, 2025 |title=Checking for Errors |url=https://www.gnu.org/software/libc/manual/html_node/Checking-for-Errors.html |website=gnu.org |publisher=Free Software Foundation |access-date=Apr 6, 2025}} {{cite web |last1=Pappas|first1=Brent|last2=Gazzillo|first2=Paul |date=Jan 18, 2024 |title=Semantic Analysis of Macro Usage for Portability |url=https://arxiv.org/html/2401.10422v1 |website=arxiv.org |location=University of Central Florida, Orland Florida |publisher= |access-date=Apr 5, 2025}}

See also

References

{{reflist}}

Bibliography

  • {{cite book |first1=W. Richard |last1=Stevens |first2=Stephen A.|last2=Rago |title=Advanced Programming in the UNIX Environment|date=May 24, 2013 |publisher=Addison-Wesley Professional |isbn=978-0321637734 |edition=Third |url=http://www.kohala.com/start/apue.html |access-date=27 February 2015 }}