Copyright © tutorialspoint.com

C - perror function

previous

Synopsis:

#include <stdio.h>
#include <errno.h>

void perror(const char *s);

Description:

The perror() function shall map the error number accessed through the symbol errno to a language-dependent error message, which shall be written to the standard error stream as follows:

  • First (if s is not a null pointer and the character pointed to by s is not the null byte), the string pointed to by s followed by a colon and a <space>.

  • Then an error message string followed by a <newline>.

Simply perror will retun a message corresponding to errno. The errno is a special system variable that is set if a system call cannot perform its set task.

Return Value

The perror() function shall not return a value.

Example

#include <stdio.h>
#include <errno.h>

extern int errno ;

int main ()
{
  FILE * pFile;
  pFile = fopen ("unexist.ent","rb");
  if (pFile == NULL)
  {
    perror ("The following error occurred");
    printf( "Value of errno: %d\n", errno );
  }
  else
    fclose (pFile);
  return 0;
}

If file unexist.ent does not exit then it will produce following result:

The following error occurred: No such file or directory
Value of errno: 29

previous

Copyright © tutorialspoint.com