perror function in C explanation with example

How to use perror() function in C :

_perror _method of C comes in handy if you want to print an error message to the user. It shows the most recent error that was occurred before the function was called. Basically, it prints one error message to stderr. This function is defined as below :

void perror(const char *str)

As you can see, it takes one string as the parameter. This is the custom message that you want to print with the final error message. First, the string parameter is printed, followed by a colon and then space and finally the most recent error. If we call this method without any error, it will print as an undefined error.

To use this function, we don’t need any header file to include. But you can also print the error number errno by including errno.h header file.

One thing we should keep in mind that _perror _should be called immediately after an error has occurred. Else it may alter the errno value. This function returns no value or void.

perror Example program :

#include <stdio.h>
#include <errno.h>
int main()
{
    //1
    perror("error found");
    printf("errno = %d\n", errno);
    FILE *f;
    //2
    if ((f = fopen("file.txt", "r")) == NULL)
    {
        //3
        perror("Error opening the file");
        printf("errno = %d\n", errno);
    }
    //4
    perror("error found");
    printf("errno = %d\n", errno);
}

It will print the below output :

error found: Undefined error: 0
errno = 0
Error opening the file: No such file or directory
errno = 2
error found: No such file or directory
errno = 2

Explanation :

The commented numbers in the above program denote the step numbers below :

  1. We are using perror() to print one error message. We are also printing the error number errno variable. As no error is reported in the program yet, it will print Undefined error and 0 as the error number.

  2. Create one file pointer f and try to open one file. This file doesn’t exist, so it will return NULL and move inside the if statement.

  3. Inside the if statement, we are printing the error message using perror again. The parameter is ‘Error opening the file’, but the print out message is ‘Error opening the file: No such file or directory’. So, ‘No such file or directory’ is the message printed by the perror function. It also printed the errno as 2.

  4. In this step, we are printing the error number and the message like the first step. But you can see that the messages are different this time. This is because_ perror()_ prints the last error message reported and since the last error message was reported ‘No such file or directory’, it printed out the same.

c perror example

You can fork this example on Github.

Conclusion :

Using perror() in a program is a good development practice. We can use this method to print a custom error message with a lot more information. The only thing that we will have to keep in mind is that this function prints out the_ last reported error message_ in the system. So, if we don’t put it in the proper position, it may produce misleading outputs.