Home >>C Tutorial >C Error Handling

Error Handling in C

Error Handling in C

As a matter of fact, the direct support of the error handling in C language is not provided. However, being a programming language access of the lower level in the form of variables is provided by the C language. In case of any error, most of the functions of C or even Unix calls return -1 or NULL and errno is set that is an error code. This basically indicates the occurrence of an error during any function call and set as a global variable. Various error codes can be found that are defined in the <error.h> header file.

This helps the programmer in C to check the returned values and act accordingly. If errno is displaying the value 0, then it is confirmed that there is no error in the program.

Let's understand the types of functions in errno

There are basically two types of functions that are defined in errno that are used to display the text message that are associated with errno:

  • Perror()
  • Strerror()

The perror() : function is used to display the string that is passed to it by the user and that string is followed by a colon, a space and finally the textual representation of the of the current errno value.

The strerror() :function generally returns a pointer to the current representation of the errno value.

#include <stdio.h>
#include <errno.h>
#include <string.h>

extern int errno ;

int main () {

   FILE * pf;
   int errnum;
   pf = fopen ("unexist.txt", "rb");
	
   if (pf == NULL) {
   
      errnum = errno;
      fprintf(stderr, "Value of errno: %d\n", errno);
      perror("Error printed by perror");
      fprintf(stderr, "Error opening file: %s\n", strerror( errnum ));
   } else {
   
      fclose (pf);
   }
   
   return 0;
}
Output :
Value of errno: 2
Error printed by perror: No such file or directory
Error opening file: No such file or directory

Let's understand the divide by zero errors

As it is a common problem with the programmers hence, it is recommended to check that if a divisor is zero or not at the time of dividing any number. Or else, it will create a runtime error.

Live Demo
#include <stdio.h>
#include <stdlib.h>

main() {

   int dividend = 20;
   int divisor = 0;
   int quotient;
 
   if( divisor == 0){
      fprintf(stderr, "Division by zero! Exiting...\n");
      exit(-1);
   }
   
   quotient = dividend / divisor;
   fprintf(stderr, "Value of quotient : %d\n", quotient );

   exit(0);
}
Output :
Division by zero! Exiting...

Let's understand the program exit status

It is common that a program coming out after a successful operation exits with a value of EXIT_SUCCESS. Please note that EXIT_SUCCESS is a macro and generally defined by 0.

It is recommended that the programmer should exit the program with a status EXIT_FAILURE that is defined as -1 in the case of an error condition in the program.

Live Demo
#include 
#include 

main() {

   int dividend = 20;
   int divisor = 5;
   int quotient;
 
   if( divisor == 0) {
      fprintf(stderr, "Division by zero! Exiting...\n");
      exit(EXIT_FAILURE);
   }
	
   quotient = dividend / divisor;
   fprintf(stderr, "Value of quotient : %d\n", quotient );

   exit(EXIT_SUCCESS);
}
Output :Value of quotient : 4

No Sidebar ads