Defined in header <errno.h> | ||||
---|---|---|---|---|
|
errno
is a preprocessor macro that expands to a thread-local (since C11) modifiable lvalue of type int
. Several standard library functions indicate errors by writing positive integers to errno
. Typically, the value of errno
is set to one of the error codes listed in <errno.h>
as macro constants beginning with the letter E
followed by uppercase letters or digits.
The value of errno
is 0
at program startup, and although library functions are allowed to write positive integers to errno
whether or not an error occurred, library functions never store 0
in errno
.
Library functions perror
and strerror
can be used to obtain textual descriptions of the error conditions that correspond to the current errno
value.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include <stdio.h> #include <math.h> #include <errno.h> void show_errno( void ) { if ( errno ==EDOM) printf ( "domain error" ); if ( errno ==EILSEQ) printf ( "illegal sequence" ); if ( errno ==ERANGE) printf ( "pole or range error" ); if ( errno ==0) printf ( "no error" ); printf ( " occurred\n" ); } int main( void ) { printf ( "MATH_ERRNO is %s\n" , math_errhandling & MATH_ERRNO ? "set" : "not set" ); errno = 0; 1.0/0.0; show_errno(); errno = 0; acos (+1.1); show_errno(); errno = 0; log (0.0); show_errno(); errno = 0; sin (0.0); show_errno(); } |
Output:
1 2 3 4 5 | MATH_ERRNO is set pole or range error occurred domain error occurred pole or range error occurred no error occurred |
References
- C11 standard (ISO/IEC 9899:2011):
- 7.5 Errors <errno.h> (p: 205)
- K.3.1.3 Use of errno (p: 584)
- K.3.2 Errors <errno.h> (p: 585)
- C99 standard (ISO/IEC 9899:1999):
- 7.5 Errors <errno.h> (p: 186)
- C89/C90 standard (ISO/IEC 9899:1990):
- 4.1.3 Errors <errno.h>
See also
macros for standard POSIX-compatible error conditions (macro constant) | |
displays a character string corresponding of the current error to stderr (function) | |
(C11)(C11) | returns a text version of a given error code (function) |
(C99)(C99)(C99) | defines the error handling mechanism used by the common mathematical functions (macro constant) |
Please login to continue.