The perfect place for easy learning...

C Programming Language


Topics List

Place your ad here

Error Handling in C

C programming language does not support error handling that are occured at program execution time. However, C provides a header file called error.h. The header file error.h contains few methods and variables that are used to locate error occured during the program execution. Generally, c programming function returns NULL or -1 in case of any error occured, and there is a global variable called errno which stores the error code or error number. The following table lists few errno values and thier meaning.

Error Number Meaning
1 Specified operation not permitted
2 No such file or directory.
3 No such process.
4 Interrupted system call.
5 IO 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 programming language provides the following two methods to represent errors occured during program execution.

  • perror( )
  • strerror( )

perror( ) - The perror() function returns a string passed to it along with the textual representation of current errno value.

strerror( ) - The strerror() function returns a pointer to the string representation of the current errno value. This method is defined in the header file string.h

Consider the following example program...

Example Program to illustrate error handling in C.


int main(){

    FILE *f_ptr;
    f_ptr = fopen("abc.txt", "r");
    if(f_ptr == NULL){
        printf("Value of errno: %d\n ", errno);
        printf("The error message is : %s\n", strerror(errno));
        perror("Message from perror");
        printf("File is opened in reading mode!");
    return 0;


Place your ad here
Place your ad here