# **[](https://en.cppreference.com/w/c/header/errno)** provides `errno`, a thread-local variable that functions set to indicate why they failed. Common codes: `ENOENT` (no file), `EACCES` (permission denied), `EINVAL` (invalid argument). Only check `errno` immediately after a function that failed; successful calls may overwrite it, giving you meaningless noise. ## Example This example checks errno to distinguish different error conditions after failed file operations. ```c // compile: gcc -o errnoexample errnoexample.c // run: ./errnoexample // description: interpret errno after system call failures #include #include #include #include int main() { FILE* f = fopen("/nonexistent/file.txt", "r"); if (!f) { printf("fopen failed: %s (errno %d)\n", strerror(errno), errno); } if (access("/etc/shadow", R_OK) < 0) { printf("access denied: %s\n", strerror(errno)); } return 0; } ``` ## Common macros and functions **Error reporting**: - `errno`: thread-local variable holding last error code (set by library functions on error) - `strerror(code)`: convert errno value to human-readable string - `perror(prefix)`: print `prefix: error message` to stderr (from ``) **Common error codes** (POSIX): - `ENOENT`: no such file or directory - `EACCES`: permission denied - `EINVAL`: invalid argument - `ENOMEM`: out of memory (malloc, etc.) - `EEXIST`: file exists - `EISDIR`: is a directory (expected file) - `ENOTDIR`: not a directory (expected directory) - `EPERM`: operation not permitted - `EAGAIN`, `EWOULDBLOCK`: resource temporarily unavailable - `EINTR`: interrupted system call - `EBADF`: bad file descriptor - `ERANGE`: result out of range - `EIO`: input/output error - `ENOSPC`: no space left on device **Usage notes**: - Check errno **immediately** after suspect function (other functions may overwrite it) - errno is meaningful only when function signals error (check return value first) - Always `#include ` before using errno - errno is never set to 0 by functions (only by your code)