perror Function

Prints an error message to stderr.

Include

<stdio.h>

Prototype

void perror(const char * s);

Argument

s
string to print

Return Value

None.

Remarks

The string s is printed followed by a colon and a space. Then, an error message based on errno is printed followed by an newline.

Example

#include <stdio.h>

int main(void)
{
  FILE *myfile;

  if ((myfile = fopen("samp.fil", "r+")) == NULL)
    perror("Cannot open samp.fil");
  else
    printf("Success opening samp.fil\n");

  fclose(myfile);
}

Example Output

Cannot open samp.fil:  file open error