Previous Page
Next Page


Obtains a string that describes a given error

#include <string.h>
char *strerror ( int errornumber  );

The strerror( ) function returns a pointer to an error message string that corresponds to the specified error number. The argument value is usually that of the errno variable, but can be any integer value. The string pointed to by the return value of strerror( ) may change on successive strerror( ) calls.


FILE *fp;
char msgbuf[1024] = { '\0' };

/* Open input file: */
if (( fp = fopen( "nonexistent", "r" )) == NULL)
  int retval = errno;
  snprintf( msgbuf, sizeof(msgbuf),
            "%s:  file %s, function %s, line %d: error %d, %s.\n",
            argv[0], _  _FILE_  _, _  _func_  _, _  _LINE_  _, retval,
            strerror( retval ));
  fputs( msgbuf, stderr );
  return retval;

This error-handling block prints the following message:

./strerror:  file strerror.c, function main, line 17: error 2, No such file or directory.

See Also

perror( )

Previous Page
Next Page