Errno.h
Template:Short description
Template:C Standard Library
Template:Lowercase title
Template:Mono 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").[1]
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.[2] Most functions indicate that they detected an error by returning a special value, typically [[Null pointer|Template:Mono]] for functions that return pointers, and Template:Mono for functions that return integers. A few functions require the caller to preset errno to Template:Mono and test it afterwards to see if an error was detected.
The errno macro expands to an lvalue with type <syntaxhighlight lang="c" inline>int</syntaxhighlight>, sometimes with the <syntaxhighlight lang="c" inline>extern</syntaxhighlight> and/or <syntaxhighlight lang="c" inline>volatile</syntaxhighlight> type specifiers depending upon the platform.[3] 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:[2]
| Template:Mono | A parameter was outside a function's domain, e.g. <syntaxhighlight lang="c" class="" style="" inline="1">sqrt(-1)</syntaxhighlight> |
| Template:Mono | A result outside a function's range, e.g. <syntaxhighlight lang="c" class="" style="" inline="1">strtol("0xfffffffff", NULL, 0)</syntaxhighlight> on systems with a 32-bit wide long
|
| Template:Mono | (Required since 1994 Amendment 1 to C89 standard)[4] Illegal byte sequence, e.g. <syntaxhighlight lang="c" class="" style="" inline="1">mbstowcs(buf, "\xff", 1)</syntaxhighlight> 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 <syntaxhighlight lang="text" class="" style="" inline="1">EACCES</syntaxhighlight> for when a file cannot be opened for reading.[5] C++11 additionally defines many of the same values found within the POSIX specification.[6]
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).Template:Sfn
An <syntaxhighlight lang="text" class="" style="" inline="1">errno</syntaxhighlight> can be translated to a descriptive string using Template:Mono (defined in [[string.h|Template:Mono]]) or a BSD extension called <syntaxhighlight lang="text" class="" style="" inline="1">sys_errlist</syntaxhighlight>. The translation can be printed directly to the standard error stream using Template:Mono (defined in [[stdio.h|Template:Mono]]). As <syntaxhighlight lang="text" class="" style="" inline="1">strerror</syntaxhighlight> in many Unix-like systems is not thread-safe, a thread-safe version <syntaxhighlight lang="text" class="" style="" inline="1">strerror_r</syntaxhighlight> is used, but conflicting definitions from POSIX and GNU makes it even less portable than the <syntaxhighlight lang="text" class="" style="" inline="1">sys_errlist</syntaxhighlight> table.[7]
POSIX errors
The GNU C library (GLIBC) provides the additional POSIX error values macros in the header file Template:Mono.[8] These are the descriptions of the macros provided by Template:Mono.[9]
The macro names and meanings for error codes are defined in the POSIX Standards definition however the numeric values are NOT, though by convention the values appear to be the same across different versions of Unix.Template:Citation needed 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.[11][12]
See also
References
Bibliography
External links
- Template:Man
- Template:Man
- GNU C library manual: Error codes
- Lists of errno values on Linux, both numeric and symbolic
- ↑ International Standard for Programming Language C (C11), ISO/IEC 9899:2011, p. 205
- ↑ 2.0 2.1 International Standard for Programming Language C (C99), ISO/IEC 9899:1999, p. 186
- ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
- ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
- ↑ Template:Man
- ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
- ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
- ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
- ↑ 9.0 9.1 FreeBSD System: Error codes
- ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
- ↑ {{#invoke:citation/CS1|citation |CitationClass=web }}
- ↑ Template:Cite book