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()
void perror(const char *str);
Parameters
Parameter | Description |
---|---|
str | A 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
#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:
- The program attempts to open a file named
"nonexistent.txt"
in read mode. - If the file does not exist,
fopen()
returnsNULL
and setserrno
to indicate the error. perror()
is then called with a custom message, which prints the custom message followed by the error description corresponding to the current value oferrno
.
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
#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:
- The program attempts to open a file that does not exist.
- If
fopen()
fails, it returnsNULL
and setserrno
. perror()
is then called withNULL
as its argument, so only the error message corresponding toerrno
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
#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:
- The program attempts to close the standard output stream using
fclose()
. - If
fclose()
fails, it returns a nonzero value and setserrno
. perror()
is then called with a custom message to print the error description related to the failure of closingstdout
.
Program Output:
Error closing stdout: [error description]