Error Handling in C

There is no specific support for error handling in 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 error in programming.


 


Below is the list of error numbers and their meaning:

errno 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

Coding Tag

Coding Tag always happy to serve you in Information Technology (IT). Learn HTML, CSS, Javascript, PHP, Android, Digital Marketing, Latest IT News and many more.

Leave a Reply

avatar
  Subscribe  
Notify of