Error Handling in C
×


Error Handling in C

3332

There is no specific support for error handling in the C program. However, there are some methods defined to find out the error using the return statement. In case of any error in C language, a function returns -1 or NULL value.

A global variable errno is set for the error code. This can be used to check errors in programming.

Below is the list of error numbers and their meaning
:

error valueerror
1no operation is permitted
2no such file or directory
3no such process
4interrupted system call
5I\O error
6no such device or address
7argument list too long
8exec format error
9bad file number
10no child process
11try again
12out of memory
13permission denied
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.text", "r");
printf("Value of errno: %d\n ", errno);
printf("The error message is : %s\n", sterror(errno));
perror("Message from perror");

return 0;
}

Result:

Value of errno: 2

The error message is: No such file or directory

Message from perror: No such file or directory



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments