Errno in C

C language does not directly allow error management but it does enable access at a lesser level via return values. In the event of inaccuracy, the majority of C or perhaps even Linux function calls yield -1 or Void and set the errno error code. It’s a universal constant that shows when a function call has failed. In the header file, you’ll find a list of different error codes. As a result, a C developer can examine the supplied values and, based on them, take necessary measures. There seem to be no trouble in the program if the result is 0. Within this article today, we will be discussing the use of the “errno” variable to get error codes and messages on our Linux environment (i.e., Ubuntu 20.0.4 system.) Let’s get started with the opening of a terminal shell in the Ubuntu 20.04 Linux operating system and installing the GCC compiler of C.

Before moving towards the creation of a C code, we need a C file in our Linux system. Thus, we have been using the touch instruction to create one. This file must contain a “c” extension at its end. As you can find out, we have built a file named “errno.c”. The list command has been used to list all the files and folders of our current working directory in Linux. The newly made C file “errno.c” is also listed here. To create C code in the file, we must open it in some editor like a text editor, Vim editor, or Gnu Nano editor. So, we have been opening it inside the GNU Nano editor with the use of the “nano” command in the shell. This file will be opened in a terminal-like editor.

Example 01

Let’s begin with the initial example of this article. Let’s say, you want to open and read the data from a specific text file residing in the same current directory using the filing in C code. For this, we must use the standard input output library of a C in our code. So, we have included it at the opening line employing the “#include” keyword.

After that, we need to use the “errno.h” header in this C code so that we can utilize the built-in variable “errno” to get the error code and message. The main execution will be started from the “main()” function of the C program. Within this main program, we have been using the “FILE” variable to initialize the file descriptor “f” of pointer type. This file descriptor has been used to open the file “new.txt” in a read-only format using the “fopen” function of filing in C. The if-else statement is here to check whether the file is successfully opened or not. The “if” part of the statement will check if the returned result of the file descriptor is equal to 0 or not. If 0, then it means the file is missing or not opened successfully. Thus, the printf statement will be using the “errno” variable to display the error code.

On the other hand, the “else” part will be utilized when the condition doesn’t satisfy the file descriptor value other than. The printf statement will display that the opening is successful.

We have compiled this code with the “gcc” compiler and then executed it with the “./a.out” instruction. This code execution returns the error code number 2 where no file has been found.

To remove this error, we need to create a new text file with the “touch” instruction and add some data to it with the “echo” statement as shown. The “cat” instruction is showing that the file contains the newly added text data in it.

Now, when you compile and run the same code in the shell, it will execute the “else” part and display the message “Successful” (i.e., file got found now.)

If you want to display the error message along with its error code, you need to use the “strerror” function in the program. For that, you need to use the “string.h” header in the code as well. So, we have added the header and updated the “if” component of the “if-else” statement. The strerror() function call has been suing the “errno” to get the error message by its error code.

When your file is missing, you will be displayed with the error message “No such file or directory” at the shell as shown.

Example 02

Let’s check the error number for another error example. We have been using the standard libraries in the program and the main() function contains the initialization of a variable “val” and function call to “check” function by passing it the “val” variable. The check() function would be using the “if-else” statement to check if the value “val” is zero or not and hence the other value can be divisible by it or not. As the value “val” is 7, it can be divisible by 8. So the “else” part of the check() function would be executed to calculate the division result and display it on the shell.

After the compilation and execution, we have found that the division is possible. The else part has calculated and displayed the division value between 89 and value “7” for variable “val”.

Let’s update the code again and add 0 as a value to the variable “val” in the main() function to reverse the results. So, we have updated the “val” value to 0 as indicated in the image below. The remaining the code remained untouched.

After running this updated code, we have the error code 0 and its error message. 


This is about the use of the “errno” variable in the article to get the error codes of different exceptions using its “errno.h” header. Also, we have discussed the use of “strerror” function to display the error message along with the code number. We have incorporated two instances in this guide to better explain the idea of errno.

Source link