C programming error handling

C Programming Error Handling

Since C programming has no direct support for error handling, but being a system programming language, it provides access to lower levels based on the return value.

For any error, most C or even Unix function calls -1or NULLreturns errnoset as the error code. It is set as a global variable and indicates that an error has occurred during the function call.

You may see different types of error codes that <errno.h> are defined in the header file.

So a C programmer can check the return value and perform the appropriate action based on the return value.

errnoIt is best to set its value to 0 (zero) during program initializing . A value of 0 indicates that there are no errors in the program.

C Programming Error Handling

C errno, perror() and strerror() functions

C programming language perror()and strerror()provides two functions with which errnoto display text messages associated with it.

  1.  perror()errnoThe function displays the textual representation for the string passed to it, followed by the colon(:) and its current value.
  2.  strerror() The function errnoreturns a pointer to the textual representation of its current value.

Let’s try to open a non-existent file and throw an error. Here we have used both functions to demonstrate usage.

But you can use one or more methods of your choice to show the error. The second important point is that the file stream stderrshould be used to output all errors.

				
					#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;
}
				
			

When the above program is compiled and executed, the output will be as follows:

				
					Value of errno: 2
Error printed by perror: No such file or directory
Error opening file: No such file or directory
				
			

Error resulting from division by 0 (zero).

Oftentimes programmers do not check if the divisor is zero when dividing a number, which ultimately produces a runtime error.

The following program shows the solution to this problem by checking whether the divisor is 0 (zero) before dividing:

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

int 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);
}
				
			

When the above program is compiled and executed it will show the output as below:

				
					Division by zero! Exiting...

				
			

Error resulting from division by 0 (zero).

After the program completes successfully, the program EXIT_SUCCESSexits with its status value. Here EXIT_SUCCESSis the macro and it is denoted by 0.

If there is an error in your program and you want to exit the program, EXIT_FAILUREyou must exit the program with a status that is denoted by -1.

So let’s write the above program as follows:

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

int 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);
}
				
			

When the above program is compiled and executed, it will show the following output:

				
					Value of quotient : 4

				
			

Leave a Comment

Your email address will not be published. Required fields are marked *