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()
    perror("error found");
    printf("errno = %d\n", errno);
    FILE *f;
    if ((f = fopen("file.txt", "r")) == NULL)
        perror("Error opening the file");
        printf("errno = %d\n", errno);
    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.