perror Subroutine
Purpose
Writes a message explaining a subroutine error.
Library
Standard
C Library (libc.a
)
Syntax
#include <errno.h>
#include <stdio.h>
void perror ( String)
const char *String;
extern int errno;
extern char *sys_errlist[ ];
extern int sys_nerr;
Description
The perror subroutine writes a message on the standard error output that describes the last error that is encountered by a system call or library subroutine. The error message includes the String parameter string followed by a : (Colon), a space character, the message, and a new-line character. The String parameter string should include the name of the program that caused the error. The error number is taken from the errno global variable, which is set when an error occurs but is not cleared when a successful call to the perror subroutine is made.
To simplify various message formats, an array of message strings is
provided in the sys_errlist
structure or use the errno global
variable as an index into the sys_errlist
structure to get the message string
without the new-line character. The largest message number that is provided in the table is
sys_nerr
. Be sure to check the sys_nerr
structure because new
error codes can be added to the system before they are added to the table.
The perror subroutine retrieves an error message based on the language of the current locale.
After successfully completing, and before a call to the exit or
abort subroutine or the completion of the fflush or
fclose subroutine on the standard error stream, the perror
subroutine marks for update the st_ctime
and st_mtime
fields of
the file associated with the standard error stream.
Parameter
Item | Description |
---|---|
String | Specifies a parameter string that contains the name of the program that caused the error.
The ensuing printed message contains this string, a : (Colon), and an explanation
of the error. |