Skip to content
Tutorials Center
My learning

Topics

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.

C
#include <errno.h>

errno = 0;
long v = strtol(text, &end, 10);
if (errno == ERANGE) { /* the number was too large */ }

Try it

C Which function sets what

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.

C Using errno correctly: reset, check, save

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

C
#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 errno only when the return value says that the call failed. The exceptions are functions with no error return, such as strtol (checks errno after setting it to zero) and strtod.
  • Set errno = 0 before calls whose only error signal is errno. Otherwise a leftover error from earlier code looks like a new one.
  • Save it immediately. printf, fprintf and even free may change errno. 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") or perror("prefix"), which writes prefix: message to stderr. For thread-safe text use strerror_r (POSIX; there are two incompatible versions) or strerror_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 (printf reports through its return value, malloc returns NULL and sets ENOMEM only on POSIX systems).
  • Math functions depend on math_errhandling. If it lacks MATH_ERRNO (as on some platforms), math errors are only reported through floating-point exceptions; see fenv.h.
  • Threads: errno is 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, EDOM and ERANGE are C89, EILSEQ is C95. C23 adds nothing new here.