strerror() — Get pointer to runtime error message

Standards

Standards / Extensions C or C++ Dependencies

ISO C
POSIX.1
XPG4
XPG4.2
C99
Single UNIX Specification, Version 3

both  

Format

#include <string.h>

char *strerror(int errnum);

General description

Maps the error number in errnum to an error message string. The errnum must be a valid errno value.

This function does not produce a locale-dependent error message string.

Returned value

strerror() returns a pointer to the string, which may be overwritten by a subsequent call to strerror().

Note: Do not allow the content of this string to be modified by the program.

Example

/* This example opens a file and prints a runtime error message if an
   error occurs.
 */
#include <stdio.h>
#include <string.h>
#include <errno.h>

int main(void)
{
   FILE *stream;
⋮
   if ((stream = fopen("myfile.dat", "r")) == NULL)
      printf(" %s \n", strerror(errno));
}

Related information