Skip to content
Tutorials Center
My learning

Topics

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.

C
#include <string.h>

char *strerror(int errnum);

Try it

C Messages for common error codes

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.

C Reporting a failed call

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

C
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 errno right after the failing call and copy it to a variable if you do other work (like printing) before using it. Many functions, including printf, may change errno.
  • Only some functions set errno. Check the documentation of the function that failed, and set errno = 0 before calling functions such as strtol that 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 strerror to be thread-safe. POSIX adds strerror_r(), which has two incompatible forms (GNU returns char *, XSI returns int), and C11 Annex K has the optional strerror_s. In single-threaded programs plain strerror is 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") prints open: followed by the message for the current errno.
  • errno is not an error flag. A successful call may leave errno nonzero, so only inspect it after a function reported failure.
  • The error constants (ENOENT, EINVAL, ...) live in <errno.h>; the C standard requires only EDOM, EILSEQ and ERANGE. The rest come from POSIX. See errno.
  • Print errors to stderr in real programs, so they are not mixed into normal output.