perror, _wperror

Print an error message.

Syntax

void perror(
   const char *message
);
void _wperror(
   const wchar_t *message
);

Parameters

message
String message to print.

Remarks

The perror function prints an error message to stderr. _wperror is a wide-character version of _perror; the message argument to _wperror is a wide-character string. _wperror and _perror behave identically otherwise.

By default, this function's global state is scoped to the application. To change this behavior, see Global state in the CRT.

Generic-text routine mappings

TCHAR.H routine _UNICODE and _MBCS not defined _MBCS defined _UNICODE defined
_tperror perror perror _wperror

message is printed first, followed by a colon, then by the system error message for the last library call that produced the error, and finally by a newline character. If message is a null pointer or a pointer to a null string, perror prints only the system error message.

The error number is stored in the variable errno (defined in ERRNO.H). The system error messages are accessed through the variable _sys_errlist, which is an array of messages ordered by error number. perror prints the appropriate error message using the errno value as an index to _sys_errlist. The value of the variable _sys_nerr is defined as the maximum number of elements in the _sys_errlist array.

For accurate results, call perror immediately after a library routine returns an error. Otherwise, subsequent calls can overwrite the errno value.

In the Windows operating system, some errno values listed in ERRNO.H are unused. These values are reserved for use by the UNIX operating system. See errno, _doserrno, _sys_errlist, and _sys_nerr for a listing of errno values used by the Windows operating system. perror prints an empty string for any errno value not used by these platforms.

Requirements

Routine Required header
perror <stdio.h> or <stdlib.h>
_wperror <stdio.h> or <wchar.h>

For more compatibility information, see Compatibility.

Libraries

All versions of the C run-time libraries.

Example

// crt_perror.c
// compile with: /W3
/* This program attempts to open a file named
* NOSUCHF.ILE. Because this file probably doesn't exist,
* an error message is displayed. The same message is
* created using perror, strerror, and _strerror.
*/

#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <share.h>

int main( void )
{
   int  fh;

   if( _sopen_s( &fh, "NOSUCHF.ILE", _O_RDONLY, _SH_DENYNO, 0 ) != 0 )
   {
      /* Three ways to create error message: */
      perror( "perror says open failed" );
      printf( "strerror says open failed: %s\n",
         strerror( errno ) ); // C4996
      printf( _strerror( "_strerror says open failed" ) ); // C4996
      // Note: strerror and _strerror are deprecated; consider
      // using strerror_s and _strerror_s instead.
   }
   else
   {
      printf( "open succeeded on input file\n" );
      _close( fh );
   }
}
perror says open failed: No such file or directory
strerror says open failed: No such file or directory
_strerror says open failed: No such file or directory

See also

Process and environment control
clearerr
ferror
strerror, _strerror, _wcserror, __wcserror