Skip to content
Tutorials Center
My learning

Topics

C Reference ยท stdio.h

C perror() function

Reference for C perror(): print a prefix and the text for the current errno value to stderr, when errno is valid, and how it compares with strerror().

perror() prints a short, human-readable error message to standard error. It takes the current value of errno, looks up its text, and writes your prefix, a colon and a space, the text, and a newline. It is the quickest way to turn a failed fopen() or malloc() into a useful message.

The output looks like report.txt: No such file or directory. The prefix tells the reader what you were doing; the text tells them why it failed.

C
#include <stdio.h>

void perror(const char *prefix);

Try it

C Reporting a failed fopen

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Choosing errno values and saving them

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
void perror(const char *prefix);
Name Description
prefix A string printed first, followed by ": ". If it is NULL or empty, only the message is printed.

Output: prefix: message\n on stderr, where the message is the same text that strerror(errno) returns.

Returns: nothing.

Errors / undefined behavior: none for valid arguments. A non-terminated prefix is undefined behavior. The text of the message depends on the system and the locale; the standard does not fix the wording.

Usage notes

  • errno is only meaningful right after a failure. Library functions set it when they fail, but they are allowed to change it even when they succeed. Test the return value first (NULL, -1, EOF), then call perror() immediately.
  • Set errno = 0 before calls such as strtol() and strtod() that signal errors only through errno, then check it afterwards.
  • Other calls can overwrite it. Even printf() may change errno on failure. If you want to print something else first, copy errno into an int and restore it, as the second example shows.
  • Not every function sets errno. perror() after a failed scanf() conversion will print an unrelated or leftover message. Look up which functions set errno; the C standard library sets it in few places, POSIX in many more.
  • Output goes to stderr, which is unbuffered. It is separate from stdout, so redirecting program output to a file does not hide the error.
  • To format your own message, use fprintf(stderr, "cannot open %s: %s\n", path, strerror(errno));. This lets you add details or log to another stream. strerror() returns a pointer that may be overwritten by the next call and is not guaranteed to be thread-safe; POSIX provides strerror_r().
  • Exit codes. After reporting, a command-line program should return a non-zero status such as EXIT_FAILURE.
  • Locale. The message can be translated by the library when the locale is set; do not parse it in code.