PROLOG |NAME |SYNOPSIS |DESCRIPTION |RETURN VALUE |ERRORS |EXAMPLES |APPLICATION USAGE |RATIONALE |FUTURE DIRECTIONS |SEE ALSO |COPYRIGHT | |
STRERROR(3P) POSIX Programmer's ManualSTRERROR(3P)This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.
strerror, strerror_l, strerror_r — get error message string
#include <string.h> char *strerror(interrnum); char *strerror_l(interrnum, locale_tlocale); int strerror_r(interrnum, char *strerrbuf, size_tbuflen);
Forstrerror(): The functionality described on this reference page is aligned with the ISO C standard. Any conflict between the requirements described here and the ISO C standard is unintentional. This volume of POSIX.1‐2017 defers to the ISO C standard. Thestrerror() function shall map the error number inerrnum to a locale-dependent error message string and shall return a pointer to it. Typically, the values forerrnum come fromerrno, butstrerror() shall map any value of typeintto a message. The application shall not modify the string returned. The returned string pointer might be invalidated or the string content might be overwritten by a subsequent call tostrerror(), or by a subsequent call tostrerror_l() in the same thread. The returned pointer and the string content might also be invalidated if the calling thread is terminated. The string may be overwritten by a subsequent call tostrerror_l() in the same thread. The contents of the error message strings returned bystrerror() should be determined by the setting of theLC_MESSAGES category in the current locale. The implementation shall behave as if no function defined in this volume of POSIX.1‐2017 callsstrerror(). Thestrerror() andstrerror_l() functions shall not change the setting oferrno if successful. Since no return value is reserved to indicate an error ofstrerror(), an application wishing to check for error situations should seterrno to 0, then callstrerror(), then checkerrno. Similarly, sincestrerror_l() is required to return a string for some errors, an application wishing to check for all error situations should seterrno to 0, then callstrerror_l(), then checkerrno. Thestrerror() function need not be thread-safe. Thestrerror_l() function shall map the error number inerrnum to a locale-dependent error message string in the locale represented bylocale and shall return a pointer to it. Thestrerror_r() function shall map the error number inerrnum to a locale-dependent error message string and shall return the string in the buffer pointed to bystrerrbuf, with lengthbuflen. If the value oferrnum is a valid error number, the message string shall indicate what error occurred; if the value oferrnum is zero, the message string shall either be an empty string or indicate that no error occurred; otherwise, if these functions complete successfully, the message string shall indicate that an unknown error occurred. The behavior is undefined if thelocale argument tostrerror_l() is the special locale object LC_GLOBAL_LOCALE or is not a valid locale object handle.
Upon completion, whether successful or not,strerror() shall return a pointer to the generated message string. On errorerrno may be set, but no return value is reserved to indicate an error. Upon successful completion,strerror_l() shall return a pointer to the generated message string. Iferrnum is not a valid error number,errno may be set to[EINVAL], but a pointer to a message string shall still be returned. If any other error occurs,errno shall be set to indicate the error and a null pointer shall be returned. Upon successful completion,strerror_r() shall return 0. Otherwise, an error number shall be returned to indicate the error.
These functions may fail if:EINVALThe value oferrnum is neither a valid error number nor zero. Thestrerror_r() function may fail if:ERANGEInsufficient storage was supplied viastrerrbuf andbuflen to contain the generated message string.The following sections are informative.
None.
Historically in some implementations, calls toperror() would overwrite the string that the pointer returned bystrerror() points to. Such implementations did not conform to the ISO C standard; however, application developers should be aware of this behavior if they wish their applications to be portable to such implementations.
Thestrerror_l() function is required to be thread-safe, thereby eliminating the need for an equivalent to thestrerror_r() function. Earlier versions of this standard did not explicitly require that the error message strings returned bystrerror() andstrerror_r() provide any information about the error. This version of the standard requires a meaningful message for any successful completion. Since no return value is reserved to indicate astrerror() error, but all calls (whether successful or not) must return a pointer to a message string, on errorstrerror() can return a pointer to an empty string or a pointer to a meaningful string that can be printed. Note that the[EINVAL]error condition is a may fail error. If an invalid error number is supplied as the value oferrnum, applications should be prepared to handle any of the following: 1. Error (with no meaningful message):errno is set to[EINVAL], the return value is a pointer to an empty string. 2. Successful completion:errno is unchanged and the return value points to a string like"unknownerror" or"errornumberxxx" (wherexxx is the value oferrnum). 3. Combination of #1 and #2:errno is set to[EINVAL]and the return value points to a string like"unknownerror" or"errornumberxxx"(wherexxx is the value oferrnum). Since applications frequently use the return value ofstrerror() as an argument to functions likefprintf() (without checking the return value) and since applications have no way to parse an error message string to determine whethererrnum represents a valid error number, implementations are encouraged to implement #3. Similarly, implementations are encouraged to havestrerror_r() return[EINVAL]and put a string like"unknownerror" or"errornumberxxx"in the buffer pointed to bystrerrbuf when the value oferrnum is not a valid error number. Some applications rely on being able to seterrno to 0 before calling a function with no reserved value to indicate an error, then callstrerror(errno) afterwards to detect whether an error occurred (becauseerrno changed) or to indicate success (becauseerrno remained zero). This usage pattern requires thatstrerror(0) succeed with useful results. Previous versions of the standard did not specify the behavior whenerrnum is zero.
None.
perror(3p) The Base Definitions volume of POSIX.1‐2017,string.h(0p)
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 athttp://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, seehttps://www.kernel.org/doc/man-pages/reporting_bugs.html .IEEE/The Open Group 2017STRERROR(3P)Pages that refer to this page:string.h(0p), perror(3p), setlocale(3p)
HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface. For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere. Hosting byjambit GmbH. | ![]() |