C Reference ยท ctype.h, math.h and others
C errno and error codes
Reference for errno in C: how library functions report errors, EDOM, ERANGE and ENOENT, resetting errno, saving it, strerror and thread safety.
errno is a modifiable lvalue declared in <errno.h> that library functions use to report why they failed. When a call such as fopen() returns NULL or strtol() overflows, the function stores an error number in errno. You inspect it right away, then translate it into text with strerror() or perror().
The macro behaves like a global int, but each thread has its own copy, so threads do not disturb each other. The standard defines only three codes: EDOM, ERANGE and EILSEQ. POSIX adds dozens more, such as ENOENT and EACCES.
#include <errno.h> errno = 0; long v = strtol(text, &end, 10); if (errno == ERANGE) { /* the number was too large */ }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
#include <errno.h> errno /* modifiable lvalue of type int */ #define EDOM ... /* argument outside the function's domain */ #define ERANGE ... /* result cannot be represented */ #define EILSEQ ... /* invalid multibyte or wide character sequence */
| Code | Meaning | Set by |
|---|---|---|
EDOM |
Domain error: argument outside the mathematical domain. | sqrt(-1.0), log(-1.0), acos(2.0) (when MATH_ERRNO is in math_errhandling) |
ERANGE |
Range error: result too large or too small. | strtol, strtod, exp(1000.0), pow |
EILSEQ |
Invalid byte sequence. | mbrtowc, wcrtomb, wide I/O |
ENOENT, EACCES, EEXIST, EINVAL, ENOMEM, EINTR, ... |
POSIX codes: no such file, permission denied, file exists, invalid argument, out of memory, interrupted call. | fopen, open, malloc (on POSIX), mkdir, ... |
Value: errno is 0 at program start. Library functions may set it to any nonzero value, but the standard says they never reset it to zero. Success does not clear errno.
Errors / undefined behavior: reading errno after a successful call tells you nothing, because a function may have set it internally and succeeded anyway. Defining your own macro or variable named errno is undefined behavior.
Usage notes
- Check the function's return value first, and consult
errnoonly when the return value says that the call failed. The exceptions are functions with no error return, such asstrtol(checkserrnoafter setting it to zero) andstrtod. - Set
errno = 0before calls whose only error signal iserrno. Otherwise a leftover error from earlier code looks like a new one. - Save it immediately.
printf,fprintfand evenfreemay changeerrno. Copy it into a local variable before you print or clean up:int saved = errno;. - Translate with
strerror(code)(returns a message such as "No such file or directory") orperror("prefix"), which writesprefix: messagetostderr. For thread-safe text usestrerror_r(POSIX; there are two incompatible versions) orstrerror_s(C11 annex). - The numeric values differ by platform. Compare with the symbolic names, never with literals; the message texts also vary by libc.
- Which functions set it is documented per function. Many do not (
printfreports through its return value,mallocreturnsNULLand setsENOMEMonly on POSIX systems). - Math functions depend on
math_errhandling. If it lacksMATH_ERRNO(as on some platforms), math errors are only reported through floating-point exceptions; see fenv.h. - Threads:
errnois thread-local on every mainstream platform, so each thread has its own value. - Define your own error handling rather than relying on a global: return codes or out-parameters in your own APIs make failures explicit and testable.
- Standard version:
errno,EDOMandERANGEare C89,EILSEQis C95. C23 adds nothing new here.