perror() Function

The perror() function in C is used to print a descriptive error message to the standard error output stream. It interprets the current value of the global variable errno and outputs an error message corresponding to that value. This function is particularly useful for diagnosing errors immediately after a function call fails.


Syntax of perror()

</>
Copy
void perror(const char *str);

Parameters

ParameterDescription
strA C string that is printed before the error message. If str is not a null pointer, it is followed by a colon and a space. If it is a null pointer, only the error message is printed.

Note that perror() should be called immediately after an error is detected, as subsequent function calls may change the value of errno. The error message produced by perror() is platform-dependent.

Return Value

The perror() function does not return a value.


Examples for perror()

Example 1: Displaying an Error When Failing to Open a File

This example demonstrates how perror() is used immediately after a failed attempt to open a non-existent file.

Program

</>
Copy
#include <stdio.h>
#include <errno.h>

int main() {
    FILE *fp = fopen("nonexistent.txt", "r");
    if (fp == NULL) {
        perror("Error opening file");
    } else {
        fclose(fp);
    }
    return 0;
}

Explanation:

  1. The program attempts to open a file named "nonexistent.txt" in read mode.
  2. If the file does not exist, fopen() returns NULL and sets errno to indicate the error.
  3. perror() is then called with a custom message, which prints the custom message followed by the error description corresponding to the current value of errno.

Program Output:

Error opening file: No such file or directory

Example 2: Using perror() Without a Custom Message

This example illustrates the behavior of perror() when a null pointer is passed as the custom message parameter.

Program

</>
Copy
#include <stdio.h>
#include <errno.h>

int main() {
    FILE *fp = fopen("nonexistent.txt", "r");
    if (fp == NULL) {
        perror(NULL);
    } else {
        fclose(fp);
    }
    return 0;
}

Explanation:

  1. The program attempts to open a file that does not exist.
  2. If fopen() fails, it returns NULL and sets errno.
  3. perror() is then called with NULL as its argument, so only the error message corresponding to errno is printed.

Program Output:

No such file or directory

Example 3: Demonstrating perror() After a Library Function Failure

This example demonstrates how perror() can be used to report errors produced by a standard library function. In this case, an error is simulated by deliberately passing an invalid argument to a function that sets errno.

Program

</>
Copy
#include <stdio.h>
#include <errno.h>

int main() {
    // Simulate an error by attempting to write to a read-only stream
    if (fclose(stdout) != 0) {
        perror("Error closing stdout");
    }
    return 0;
}

Explanation:

  1. The program attempts to close the standard output stream using fclose().
  2. If fclose() fails, it returns a nonzero value and sets errno.
  3. perror() is then called with a custom message to print the error description related to the failure of closing stdout.

Program Output:

Error closing stdout: [error description]