PERROR(3P)
πΈ PERROR(3P) β the POSIX Programmer's Manual page, but make it a group chat πΈ
okay bestie, sit down, hydrate, because today we're learning about perror() π
quick heads up before we even start: this page lives in the POSIX Programmer's Manual. The Linux implementation of this interface may differ (so like, go read the Linux man page for the actual Linux tea), or honestly the interface might not even be implemented on Linux at all π
β¨ PROLOG β¨
So real talk: this is a POSIX manual page. If you're on Linux and things look different? Consult the corresponding Linux manual page for details on Linux behavior, because they're not always twinning. And sometimes the interface isn't implemented on Linux at all, so vibe check your platform first π«Ά
π NAME π
perror β write error messages to standard error
that's it. that's the whole job. it writes error messages to stderr and it does it iconically β¨
π SYNOPSIS π
#include <stdio.h>
void perror(const char *s);
π¦ DESCRIPTION π¦
So the functionality on this reference page is aligned with the ISO C standard. Any conflict between what's written here and the ISO C standard is unintentional β nobody's trying to start drama. This volume of POSIX.1β2017 literally defers to the ISO C standard. periodt.
The perror() function shall map the error number accessed through the symbol errno to a language-dependent error message, and that message gets written to the standard error stream like this:
- First (if
sis not a null pointer and the character pointed to bysis not the null byte), the string pointed to bysfollowed by a<colon>and a<space>. - Then an error message string followed by a
<newline>.
So like, "malloc: " + the message + a newline. cute. chef's kiss. π
The contents of those error message strings shall be the same as those returned by strerror() with argument errno. Same strings, different delivery system, no cap.
perror() shall mark for update the last data modification and last file status change timestamps of the file associated with the standard error stream. When does that happen? At some time between its successful completion and exit(), abort(), or the completion of fflush() or fclose() on stderr. So it's giving "eventually, bestie" energy β the timing is deliberately loose π
perror() shall not change the orientation of the standard error stream. leave the stream's orientation alone, it's thriving as it is π
On error, perror() shall set the error indicator for the stream to which stderr points, and shall set errno to indicate the error. yes, it can fail, and yes, it'll tell you by setting the indicator.
And here's the big one β since no value is returned, an application wishing to check for error situations should:
- call
clearerr(stderr)before callingperror() - then, if
ferror(stderr)returns non-zero, the value oferrnoindicates which error occurred
so that's the detective workflow. clear it, call it, check it. π
π RETURN VALUE π
The perror() function shall not return a value. Like, nothing. void. it returns the vibe, not a value. π
π₯ ERRORS π₯
Refer to fputc().
The following sections are informative. (translation: the important spec stuff is done, everything after this is bonus content β¨)
π EXAMPLES π
π Printing an Error Message for a Function π
The following example replaces bufptr with a buffer that is the necessary size. If an error occurs, the perror() function prints a message and the program exits.
#include <stdio.h>
#include <stdlib.h>
...
char *bufptr;
size_t szbuf;
...
if ((bufptr = malloc(szbuf)) == NULL) {
perror("malloc"); exit(2);
}
...
see how it's just perror("malloc") and the whole error story gets printed? that's the main character moment right there π
π§ APPLICATION USAGE π§
Application writers may prefer to use alternative interfaces instead of perror(), such as strerror_r() in combination with fprintf(). so if you want more control over your output (formatting, thread safety vibes, whatever), that combo is right there for you π«Ά
β RATIONALE β
None.
(it said what it said. nothing to add. iconic.) π
π FUTURE DIRECTIONS π
None.
(future? unchanged. slay.) β¨
πΈ SEE ALSO πΈ
fprintf()fputc()psiginfo()strerror()- The Base Definitions volume of POSIX.1β2017,
<stdio.h>
π COPYRIGHT π
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
π«Ά and that's the page!! brought to you by IEEE/The Open Group, 2017 β this was PERROR(3P), from the POSIX Programmer's Manual, and honestly? she ate. go write some error messages, bestie π
β¨π
π¦ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 Β· custom
- alpine 3.23: man-pages-posix 2017a-r0 Β· custom
- alpine 3.24: man-pages-posix 2017a-r0 Β· custom
- arch rolling: man-pages 6.19-1
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 Β· SUSE-IEEE
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms