6.18.32 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. See the strerror Function for some of the error
messages commonly printed.
Example
See the notes at the beginning of this chapter or section for
information on using printf()
or scanf()
(and other functions reading and writing the stdin
or
stdout
streams) in the example code.
#include <math.h>
#include <stdio.h>
#include <errno.h>
int main(void)
{
double x,y;
errno = 0;
x = -2.0;
y = acos (x);
if (errno)
perror("Error");
printf("The arccosine of %f is %f\n", x, y);
}
Example Output
Error: Domain error
The arccosine of -2.000000 is nan