1strerror(3) Library Functions Manual strerror(3)
2
3
4
6 strerror, strerrorname_np, strerrordesc_np, strerror_r, strerror_l -
7 return string describing error number
8
10 Standard C library (libc, -lc)
11
13 #include <string.h>
14
15 char *strerror(int errnum);
16 const char *strerrorname_np(int errnum);
17 const char *strerrordesc_np(int errnum);
18
19 int strerror_r(int errnum, char buf[.buflen], size_t buflen);
20 /* XSI-compliant */
21
22 char *strerror_r(int errnum, char buf[.buflen], size_t buflen);
23 /* GNU-specific */
24
25 char *strerror_l(int errnum, locale_t locale);
26
27 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
28
29 strerrorname_np(), strerrordesc_np():
30 _GNU_SOURCE
31
32 strerror_r():
33 The XSI-compliant version is provided if:
34 (_POSIX_C_SOURCE >= 200112L) && ! _GNU_SOURCE
35 Otherwise, the GNU-specific version is provided.
36
38 The strerror() function returns a pointer to a string that describes
39 the error code passed in the argument errnum, possibly using the
40 LC_MESSAGES part of the current locale to select the appropriate lan‐
41 guage. (For example, if errnum is EINVAL, the returned description
42 will be "Invalid argument".) This string must not be modified by the
43 application, but may be modified by a subsequent call to strerror() or
44 strerror_l(). No other library function, including perror(3), will
45 modify this string.
46
47 Like strerror(), the strerrordesc_np() function returns a pointer to a
48 string that describes the error code passed in the argument errnum,
49 with the difference that the returned string is not translated accord‐
50 ing to the current locale.
51
52 The strerrorname_np() function returns a pointer to a string containing
53 the name of the error code passed in the argument errnum. For example,
54 given EPERM as an argument, this function returns a pointer to the
55 string "EPERM".
56
57 strerror_r()
58 The strerror_r() function is similar to strerror(), but is thread safe.
59 This function is available in two versions: an XSI-compliant version
60 specified in POSIX.1-2001 (available since glibc 2.3.4, but not POSIX-
61 compliant until glibc 2.13), and a GNU-specific version (available
62 since glibc 2.0). The XSI-compliant version is provided with the fea‐
63 ture test macros settings shown in the SYNOPSIS; otherwise the GNU-spe‐
64 cific version is provided. If no feature test macros are explicitly
65 defined, then (since glibc 2.4) _POSIX_C_SOURCE is defined by default
66 with the value 200112L, so that the XSI-compliant version of str‐
67 error_r() is provided by default.
68
69 The XSI-compliant strerror_r() is preferred for portable applications.
70 It returns the error string in the user-supplied buffer buf of length
71 buflen.
72
73 The GNU-specific strerror_r() returns a pointer to a string containing
74 the error message. This may be either a pointer to a string that the
75 function stores in buf, or a pointer to some (immutable) static string
76 (in which case buf is unused). If the function stores a string in buf,
77 then at most buflen bytes are stored (the string may be truncated if
78 buflen is too small and errnum is unknown). The string always includes
79 a terminating null byte ('\0').
80
81 strerror_l()
82 strerror_l() is like strerror(), but maps errnum to a locale-dependent
83 error message in the locale specified by locale. The behavior of str‐
84 error_l() is undefined if locale is the special locale object
85 LC_GLOBAL_LOCALE or is not a valid locale object handle.
86
88 The strerror(), strerror_l(), and the GNU-specific strerror_r() func‐
89 tions return the appropriate error description string, or an "Unknown
90 error nnn" message if the error number is unknown.
91
92 On success, strerrorname_np() and strerrordesc_np() return the appro‐
93 priate error description string. If errnum is an invalid error number,
94 these functions return NULL.
95
96 The XSI-compliant strerror_r() function returns 0 on success. On er‐
97 ror, a (positive) error number is returned (since glibc 2.13), or -1 is
98 returned and errno is set to indicate the error (before glibc 2.13).
99
100 POSIX.1-2001 and POSIX.1-2008 require that a successful call to str‐
101 error() or strerror_l() shall leave errno unchanged, and note that,
102 since no function return value is reserved to indicate an error, an ap‐
103 plication that wishes to check for errors should initialize errno to
104 zero before the call, and then check errno after the call.
105
107 EINVAL The value of errnum is not a valid error number.
108
109 ERANGE Insufficient storage was supplied to contain the error descrip‐
110 tion string.
111
113 For an explanation of the terms used in this section, see at‐
114 tributes(7).
115
116 ┌───────────────────┬───────────────┬──────────────────────────────────┐
117 │Interface │ Attribute │ Value │
118 ├───────────────────┼───────────────┼──────────────────────────────────┤
119 │strerror() │ Thread safety │ MT-Unsafe race:strerror │
120 ├───────────────────┼───────────────┼──────────────────────────────────┤
121 │strerrorname_np(), │ Thread safety │ MT-Safe │
122 │strerrordesc_np() │ │ │
123 ├───────────────────┼───────────────┼──────────────────────────────────┤
124 │strerror_r(), │ Thread safety │ MT-Safe │
125 │strerror_l() │ │ │
126 └───────────────────┴───────────────┴──────────────────────────────────┘
127
129 strerror()
130 C11, POSIX.1-2008.
131
132 strerror_r()
133 strerror_l()
134 POSIX.1-2008.
135
136 strerrorname_np()
137 strerrordesc_np()
138 GNU.
139
140 POSIX.1-2001 permits strerror() to set errno if the call encounters an
141 error, but does not specify what value should be returned as the func‐
142 tion result in the event of an error. On some systems, strerror() re‐
143 turns NULL if the error number is unknown. On other systems, str‐
144 error() returns a string something like "Error nnn occurred" and sets
145 errno to EINVAL if the error number is unknown. C99 and POSIX.1-2008
146 require the return value to be non-NULL.
147
149 strerror()
150 POSIX.1-2001, C89.
151
152 strerror_r()
153 POSIX.1-2001.
154
155 strerror_l()
156 glibc 2.6. POSIX.1-2008.
157
158 strerrorname_np()
159 strerrordesc_np()
160 glibc 2.32.
161
163 The GNU C Library uses a buffer of 1024 characters for strerror().
164 This buffer size therefore should be sufficient to avoid an ERANGE er‐
165 ror when calling strerror_r().
166
167 strerrorname_np() and strerrordesc_np() are thread-safe and async-sig‐
168 nal-safe.
169
171 err(3), errno(3), error(3), perror(3), strsignal(3), locale(7)
172
173
174
175Linux man-pages 6.04 2023-03-30 strerror(3)