Format
#include <stdio.h> void perror(const char *string);
Language Level: ANSI, POSIX, XPG4
perror prints an error message to stderr. If string is
not NULL and 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 by a new-line
character.
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.
Return Value
There is no return value.
Example
This example tries to open a stream. If fopen fails, the
example prints a message and ends the program.
#include <stdio.h> #include <stdlib.h>
#define FILENAME "myfile.mjq"
int main(void)
{
FILE *fh;
if ((fh = fopen(FILENAME,"r")) == NULL)
{
perror("Could not open data file");
abort();
}
return 0;
/*******************************************************
The output should be:
Could not open data file: The file cannot be found. *******************************************************/ }
![]()
clearerr -- Reset Error Indicators
ferror -- Test for Read/Write Errors
strerror -- Set Pointer to Runtime
Error Message
_strerror -- Set Pointer to System
Error String
<stdio.h>