Join the social network of Tech Nerds, increase skill rank, get work, manage projects...

 2 Answer(s)

  • In C programming we don't have the error handling mechanism but at the point of view system programming language, it provides access at lower level in the form of return values. it can return -1 and NULL in any error and sets an error code errno is set which is global variable and indicates an error occurred during any function call. the various error codes defined in header file.

    C programming language provides perror() and strerror() functions which can be used to display the text message associated with errno.

    The Below code can represent the error using perror() and strerror() function if the file pointer value indicates to NULL that means the file not present in that location or not exist.

    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 ));
          fclose (pf);
       return 0;
Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: