C errno (errno.h) Library
C errno Macros
The <errno.h> header defines errno, which stores an error code when something goes wrong, for example when a file cannot be opened.
Many functions set errno when they fail. Use strerror() or perror() to turn the code into a readable message.
Example
Print the error message when a file cannot be opened:
FILE *fptr = fopen("missing.txt", "r");
if (fptr == NULL) {
printf("Error: %s\n", strerror(errno));
}
A list of the <errno.h> macros in the C standard can be found in the table below:
| Macro | Description |
|---|---|
| errno | Stores the error code of the last error. It is 0 when the program starts. A function that works does not set it back to 0, so only check it right after a function has failed |
| EDOM | Domain error: an argument is outside the values that a math function accepts, for example sqrt(-1) |
| ERANGE | Range error: the result is too big or too small to store, for example from pow() or strtol() |
| EILSEQ | Illegal byte sequence: a character that is not valid in the current character encoding |
Other Error Codes
These error codes are not part of the C standard, but most systems (like Linux, macOS and Windows) have them:
| Macro | Description |
|---|---|
| EACCES | Permission denied |
| EINVAL | Invalid argument |
| ENOENT | No such file or directory |
| ENOMEM | Not enough memory |
Related Pages
Read more about errors in our C Error Handling Tutorial.