1PERROR(3P) POSIX Programmer's Manual PERROR(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 perror — write error messages to standard error
13
15 #include <stdio.h>
16
17 void perror(const char *s);
18
20 The functionality described on this reference page is aligned with the
21 ISO C standard. Any conflict between the requirements described here
22 and the ISO C standard is unintentional. This volume of POSIX.1‐2017
23 defers to the ISO C standard.
24
25 The perror() function shall map the error number accessed through the
26 symbol errno to a language-dependent error message, which shall be
27 written to the standard error stream as follows:
28
29 * First (if s is not a null pointer and the character pointed to by s
30 is not the null byte), the string pointed to by s followed by a
31 <colon> and a <space>.
32
33 * Then an error message string followed by a <newline>.
34
35 The contents of the error message strings shall be the same as those
36 returned by strerror() with argument errno.
37
38 The perror() function shall mark for update the last data modification
39 and last file status change timestamps of the file associated with the
40 standard error stream at some time between its successful completion
41 and exit(), abort(), or the completion of fflush() or fclose() on
42 stderr.
43
44 The perror() function shall not change the orientation of the standard
45 error stream.
46
47 On error, perror() shall set the error indicator for the stream to
48 which stderr points, and shall set errno to indicate the error.
49
50 Since no value is returned, an application wishing to check for error
51 situations should call clearerr(stderr) before calling perror(), then
52 if ferror(stderr) returns non-zero, the value of errno indicates which
53 error occurred.
54
56 The perror() function shall not return a value.
57
59 Refer to fputc().
60
61 The following sections are informative.
62
64 Printing an Error Message for a Function
65 The following example replaces bufptr with a buffer that is the neces‐
66 sary size. If an error occurs, the perror() function prints a message
67 and the program exits.
68
69
70 #include <stdio.h>
71 #include <stdlib.h>
72 ...
73 char *bufptr;
74 size_t szbuf;
75 ...
76 if ((bufptr = malloc(szbuf)) == NULL) {
77 perror("malloc"); exit(2);
78 }
79 ...
80
82 Application writers may prefer to use alternative interfaces instead of
83 perror(), such as strerror_r() in combination with fprintf().
84
86 None.
87
89 None.
90
92 fprintf(), fputc(), psiginfo(), strerror()
93
94 The Base Definitions volume of POSIX.1‐2017, <stdio.h>
95
97 Portions of this text are reprinted and reproduced in electronic form
98 from IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
99 table Operating System Interface (POSIX), The Open Group Base Specifi‐
100 cations Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of
101 Electrical and Electronics Engineers, Inc and The Open Group. In the
102 event of any discrepancy between this version and the original IEEE and
103 The Open Group Standard, the original IEEE and The Open Group Standard
104 is the referee document. The original Standard can be obtained online
105 at http://www.opengroup.org/unix/online.html .
106
107 Any typographical or formatting errors that appear in this page are
108 most likely to have been introduced during the conversion of the source
109 files to man page format. To report such errors, see https://www.ker‐
110 nel.org/doc/man-pages/reporting_bugs.html .
111
112
113
114IEEE/The Open Group 2017 PERROR(3P)