0% found this document useful (0 votes)
74 views

Error Handling in C

C does not provide built-in error handling. Functions return -1 or NULL on error and set the global errno variable to an error code. The errno variable indicates the type of error and functions like perror() and strerror() return strings describing the error. Common errors include file not found, permission denied, and out of memory. Division by zero also causes undefined behavior and should be checked for before the division occurs.

Uploaded by

Jaysinh Kumpavat
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

Error Handling in C

C does not provide built-in error handling. Functions return -1 or NULL on error and set the global errno variable to an error code. The errno variable indicates the type of error and functions like perror() and strerror() return strings describing the error. Common errors include file not found, permission denied, and out of memory. Division by zero also causes undefined behavior and should be checked for before the division occurs.

Uploaded by

Jaysinh Kumpavat
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Error Handling in C

 C language does not provide any direct support for error handling.
 However a few methods and variables defined in errno.h header file can
be used to point out error using the return statement in a function.
 In C language, a function returns -1 or NULL value in case of any error and
a global variable errno is set with the error code.
 So the return value can be used to check error while programming.

What is errno?

 Whenever a function call is made in C language, a variable named errno is


associated with it.
 It is a global variable, which can be used to identify which type of error
was encountered while function execution, based on its value.
 Below we have the list of Error numbers and what does they mean.

errno value Error


1 Operation not permitted
2 No such file or directory
3 No such process
4 Interrupted system call
5 I/O error
6 No such device or address
7 Argument list too long
8 Exec format error
9 Bad file number
10 No child processes
11 Try again
12 Out of memory
13 Permission denied

 C language uses the following functions to represent error messages


associated with errno:

a. perror(): returns the string passed to it along with the textual


representation of the current errno value.

Page 1
b. strerror() is defined in string.h library. This method returns a pointer
to the string representation of the current errno value.

An Example

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

int main ()
{
FILE *fp;

/*
If a file, which does not exists, is opened, we will get an error
*/

fp = fopen("IWillReturnError.txt", "r");

printf("Value of errno: %d\n ", errno);


printf("The error message is : %s\n", strerror(errno));
perror("Message from perror");

return 0;
}

Page 2
Other ways of Error Handling

 We can also use Exit Status constants in the exit() function to inform the
calling function about the error. The two constant values available for use
are EXIT_SUCCESS and EXIT_FAILURE.
 These are nothing but macros defined stdlib.h header file.
 It is always a good practice to exit a program with a exit status.
 EXIT_SUCCESS and EXIT_FAILURE are two macro used to show exit status.
 In case of program coming out after a successful operation EXIT_SUCCESS
is used to show successful exit. It is defined as 0.
 EXIT_FAILURE is used in case of any failure in the program. It is defined as
-1.

Division by Zero

 There are some situation where nothing can be done to handle the error.
 In C language one such situation is division by zero.
 All you can do is avoid doing this, because if you do so, C language is not
able to understand what happened, and gives a runtime error.
 Best way to avoid this is, to check the value of the divisor before using it in
the division operations.
 You can use if condition, and if it is found to be zero, just display a
message and return from the function.

Page 3

You might also like