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.
#include <stdio.h> void perror(const char *prefix);
Try it
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.
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
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
errnois 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 callperror()immediately.- Set
errno = 0before calls such asstrtol()andstrtod()that signal errors only througherrno, then check it afterwards. - Other calls can overwrite it. Even
printf()may changeerrnoon failure. If you want to print something else first, copyerrnointo anintand restore it, as the second example shows. - Not every function sets
errno.perror()after a failedscanf()conversion will print an unrelated or leftover message. Look up which functions seterrno; the C standard library sets it in few places, POSIX in many more. - Output goes to
stderr, which is unbuffered. It is separate fromstdout, 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 providesstrerror_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.