c Programming/C Reference/stdio.h/perror
From Wikibooks, open books for an open world
< C Programming | C Reference | stdio.h
|
|
A Wikibookian suggests that this book or chapter be merged into Error code. Please discuss whether or not this merge should happen on the discussion page. |
The POSIX error function, perror, is used in C and C++ to print an error message to stderr, based on the error state stored in errno.[1]It prints str and an implementation-defined error message corresponding to the global variable errno.
Contents |
History [edit]
The definition of perror was first released in Issue 1 of the System V Interface Definition.
Usage [edit]
Inclusion [edit]
#include <stdio.h>
Declaration [edit]
void perror(const char* prefix);
Example [edit]
int fd = open("/etc/passwd", O_RDONLY); if (fd == -1) { perror("open"); exit(1); }
Semantics [edit]
If the parameter prefix is non-NULL, perror will first print prefix followed by a colon and a space to standard error. Then, it will print the result of strerror to standard error, followed by a newline character. For instance the above example may print
open: Permission denied
See also [edit]
References [edit]
- ↑ ISO/IEC 9899:1999 specification. p. 305, § 7.19.10.2. http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf.