errno is a global
variable in C that is defined in the <errno.h>
header.
Although C does not provide direct support for exception handling, errno
can be used for a similar purpose.
When an error occurs, the errno variable is automatically assigned a code. This code can then be used to identify the error that has occurred.
Below is a list of some errno
codes and their meanings:
Error Code | Meaning |
1 | Operation not permitted |
2 | No such file or directory |
3 | No such process |
4 | Interrupted system call |
5 | Input/Output error |
6 | No such device or address |
7 | Argument list too long |
8 | Exec format error |
9 | Bad file number |
10 | No child processes |
#include <stdio.h>#include <errno.h>int main(){//We are trying to open a file that does not existFILE* input = fopen("NoFile.txt", "r");//Note the value in errnoprintf("\nError Code: %d\n", errno);//Print associated error messageperror("Error Message");return 0;}
In the above code, we tried to open a file that does not exist; so, the errno
variable was assigned error code 2 : No such file or directory
.
Free Resources