C Standard Library |
---|
|
Errno.h is a header file in the standard library of C programming language. It defines macros to report error conditions through error codes stored in a static location called errno
.
A value 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. 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
, containing the last error code generated in any function using the errno facility. Originally this was a static memory location, but macros are almost always used today to allow for multi-threading, each thread will see its own error number.
The header file also defines macros that expand to integer constants that represent the error codes. The C standard library only require three to be defined:
EDOM
sqrt(-1)
ERANGE
strtol("0xfffffffff",NULL,0)
EILSEQ
mbstowcs(buf,"\xff", 1)
(if the multi-byte encoding is UTF-8).POSIX compliant operating systems like UNIX or Linux include many other error values, many of which are used much more often than the above ones, such as EACCES for when a file cannot be opened for reading.