Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

What is errno in C?

Khizar Hayat Saani

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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.

RELATED TAGS

c

CONTRIBUTOR

Khizar Hayat Saani
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring