perror() — Print error message

Standards

Standards / Extensions C or C++ Dependencies

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

both  

Format

#include <stdio.h>

void perror(const char *string);

#define _OPEN_SYS_UNLOCKED_EXT 1
#include <stdio.h>

void perror_unlocked(const char *string);
X/Open:
#define _XOPEN_SOURCE_EXTENDED 1
#include <stdio.h>

void perror(const char *string);
Berkeley sockets:
#define _OE_SOCKETS
#include <stdio.h>

void perror(const char *string);

General description

Prints an error message to stderr. If string is not NULL and it does not point to a NULL character, the string pointed to by string is printed to the standard error stream, followed by a colon and a space. The message associated with the value in errno is then printed followed with the errno2 value in parenthesis and a newline character. The content of the message is the same as the content of a string returned by strerror() with the argument errno.

The perror() string shown as: EDC5121I Invalid argument. (errno2=0x0C0F8402).

To produce accurate results, you should ensure that perror() is called immediately after a library function returns with an error; otherwise, subsequent calls may alter the errno value.

If the error is associated with the stderr file, a call to perror() is not valid.

There is an environment variable _EDC_ADD_ERRNO2, which when set to 0, will remove the append of the current errno2 value at the end of the perror() string shown.

The perror() function will not change the orientation of the stderr stream.

perror_unlocked() is functionally equivalent to perror() with the exception that it is not thread-safe. This function can safely be used in a multithreaded application if and only if it is called while the invoking thread owns the (FILE*) object, as is the case after a successful call to either the flockfile() or ftrylockfile() function.

Returned value

perror() returns no values.

Example

CELEBP03
⁄* CELEBP03                                      

   This example tries to open a stream.                                         
   If the fopen() function fails, the example prints a message and ends         
   the program.                                                                 

 *⁄                                                                             
#include <stdio.h>                                                              
#include <stdlib.h>                                                             
                                                                                
int main(void)                                                                  
{                                                                               
   FILE *fh;                                                                    
                                                                                
   if ((fh = fopen("myfile.dat","r")) == NULL)                                  
   {                                                                            
      perror("Could not open data file");                                       
      abort();                                                                  
   }                                                                            
}                                                                               
The following example tries to open a stream socket. If the socket fails, the example prints a message and ends the program.
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket>

int main(void)
{
   ins s;

   if ((s = socket (AF_INET,SOCK_STREAM,0)) <0)
   {
      perror("Could not open socket");
      exit(-1);
   }
}

Related information