perror, _wperror
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

perror, _wperror

Print an error message.

void perror(
   const char *string 
void _wperror(
   const wchar_t *string 


String message to print.


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

Generic-Text Routine Mappings

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

string 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 string 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 with an error. Otherwise, subsequent calls can overwrite the errno value.

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

Note   In the Visual C++ implementation of the C run-time library, perror does not write to the same stream as stderr.


Routine Required header Compatibility
perror <stdio.h> or <stdlib.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP
_wperror <stdio.h> or <wchar.h> Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.


All versions of the C run-time libraries.


// crt_perror.c
/* 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>

int main( void )
   int  fh;

   if( (fh = _open( "NOSUCHF.ILE", _O_RDONLY )) == -1 )
      /* Three ways to create error message: */
      perror( "perror says open failed" );
      printf( "strerror says open failed: %s\n", strerror( errno ) );
      printf( _strerror( "_strerror says open failed" ) );
      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 Routines | clearerr | ferror | strerror | Run-Time Routines and .NET Framework Equivalents

© 2016 Microsoft