C Reference ยท string.h
C strerror() function
Reference for C strerror(): convert an errno value into readable text, why the wording differs by system, thread safety, strerror_r, and the perror alternative.
strerror() takes an error number, usually the value of errno, and returns a pointer to a string that describes it, such as "No such file or directory". It is how programs turn a failed fopen, strtol or malloc into something a person can read.
The text is not standardized beyond being a description, and it may be localized. Never compare it to a fixed string; compare the errno value instead.
#include <string.h> char *strerror(int errnum);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
char *strerror(int errnum);
| Name | Description |
|---|---|
errnum |
An error number, normally a value of errno. |
Returns: a pointer to a string describing the error. For an unknown number, implementations return something like "Unknown error 12345" rather than NULL.
Errors / undefined behavior: do not modify the returned string. Its contents may be overwritten by the next call to strerror (or by a call in another thread, on systems where the buffer is shared).
Usage notes
- Read
errnoright after the failing call and copy it to a variable if you do other work (like printing) before using it. Many functions, includingprintf, may changeerrno. - Only some functions set
errno. Check the documentation of the function that failed, and seterrno = 0before calling functions such asstrtolthat signal errors only through it. - Message text varies. It differs between glibc, musl, macOS and Windows, and can follow the locale. Use messages for people, error codes for logic.
- Thread safety. The standard does not require
strerrorto be thread-safe. POSIX addsstrerror_r(), which has two incompatible forms (GNU returnschar *, XSI returnsint), and C11 Annex K has the optionalstrerror_s. In single-threaded programs plainstrerroris fine. glibc's implementation is in practice safe for known codes. perror()is shorter when you only want to print to standard error:perror("open")printsopen:followed by the message for the currenterrno.errnois not an error flag. A successful call may leaveerrnononzero, so only inspect it after a function reported failure.- The error constants (
ENOENT,EINVAL, ...) live in<errno.h>; the C standard requires onlyEDOM,EILSEQandERANGE. The rest come from POSIX. Seeerrno. - Print errors to
stderrin real programs, so they are not mixed into normal output.
Related
errno,perror,strtol,fopen- Lessons: Error handling, String functions