Defines the global variable errno and symbolic error constants. Set by system calls and library functions on error. Use with perror() or strerror().
Include: #include <errno.h> | Compile: gcc -Wall -o prog prog.c
| Name | Prototype / Value | Description |
|---|---|---|
| errno | extern int errno | Global error number — set by failing system calls |
| EPERM | 1 | Operation not permitted |
| ENOENT | 2 | No such file or directory |
| EINTR | 4 | Interrupted system call |
| EIO | 5 | I/O error |
| EBADF | 9 | Bad file descriptor |
| ENOMEM | 12 | Out of memory |
| EACCES | 13 | Permission denied |
| EEXIST | 17 | File exists |
| EINVAL | 22 | Invalid argument |
| ERANGE | 34 | Result too large |
Copy, save as demo.c, and compile with the command shown at the bottom.
#include <stdio.h>
#include <errno.h>
#include <string.h>
int main(void) {
FILE *fp = fopen("/no/such/file.txt", "r");
if (fp == NULL) {
perror("fopen");
printf("errno=%d : %s\n", errno, strerror(errno));
}
errno = 0;
long val = strtol("99999999999999999", NULL, 10);
if (errno == ERANGE)
printf("Overflow! val=%ld errno=%d (%s)\n",
val, errno, strerror(errno));
return 0;
}
/* Compile: gcc -Wall -o errno_demo errno_demo.c */