What is errno in C?

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.

Some errno codes and meanings

Properties

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:

Errno codes & 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

Code

#include <stdio.h>
#include <errno.h>
int main(){
//We are trying to open a file that does not exist
FILE* input = fopen("NoFile.txt", "r");
//Note the value in errno
printf("\nError Code: %d\n", errno);
//Print associated error message
perror("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

Copyright ©2025 Educative, Inc. All rights reserved