errno
errno is a thread-local integer that library functions set when they fail. Zero means "no error recorded." A non-zero value is a code from <errno.h> such as ENOENT or ENOMEM.
Detailed Explanation
errno is not a function argument and not a return value. It is a side channel. The typical pattern is: a function returns a sentinel (NULL, -1, EOF) and, if the sentinel means failure, sets errno to say why. Success does not have to set errno to zero. A function that succeeds may leave a leftover code from an earlier failed call. The rule is: look at errno only after a documented failure, and set errno = 0 yourself before a call only when the function uses errno to report success-or-failure without a sentinel (strtol is the famous case).
The identifier errno expands to a modifiable lvalue. On a threaded implementation it is typically (*__errno_location()), so each thread has its own code. You include <errno.h>. You do not declare extern int errno;.
perror(s) writes s, a colon, and the current error text to stderr. strerror(errnum) returns a pointer to a message string, which may live in a static buffer and may not be thread-safe. POSIX strerror_r and C11 strerror_s fix that. Do not pass the strerror pointer to another thread, and do not call strerror again before you finish using the string.
Library functions are allowed to set errno even on success, except where a standard says otherwise. They are also allowed to clobber errno on failure of a function that is not the one you are checking. Save it:
int saved = errno;
fclose(fp);
errno = saved;
A signal handler that calls a library function can overwrite errno in the interrupted thread. Save and restore it in the handler if the handler does anything other than set a flag.
C itself defines a handful of codes (EDOM, ERANGE, EILSEQ). POSIX adds the ones you actually test for in systems code: EAGAIN, EINTR, ENOENT, EPERM. Compare against the macros, not against numbers. Print with perror or strerror, not with %d, when a human will read the result.
Code Examples
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fp = fopen("no-such-file.txt", "r");
if (fp == NULL) {
perror("no-such-file.txt"); /* e.g. no-such-file.txt: No such file or directory */
if (errno == ENOENT) {
return 2;
}
return EXIT_FAILURE;
}
fclose(fp);
return 0;
}#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
const char *s = "42xyz";
char *end = NULL;
errno = 0; /* strtol needs a clean slate */
long v = strtol(s, &end, 10);
if (end == s) {
fprintf(stderr, "not a number\n");
return 1;
}
if (errno == ERANGE) {
fprintf(stderr, "out of range\n");
return 1;
}
printf("%ld rest=%s\n", v, end); /* prints: 42 rest=xyz */
return 0;
}Common Gotchas
Success does not clear `errno`
a leftover `ENOENT` from an earlier call will still be sitting there. Inspect `errno` only after a failed call, or after a function documented to use it as the primary channel.
`strerror` may return a shared buffer
a second call overwrites the first string. Copy it, or use `strerror_r`.
`EINTR` means a signal interrupted a blocking call
on POSIX, a careful loop retries the call. Treating it as a hard failure drops work that would have succeeded.
Declaring `extern int errno;` yourself is wrong on threaded libcs
include `<errno.h>` so you get the per-thread accessor.