One of the many interesting functions prototyped in STDIO.H is a function called perror( ). The function prints to the stderr stream the system error message for the last library routine called that generated an error. It does this by using errno and _sys_errlist, prototyped in STDLIB.H. The _sys_errlist value is an array of error message strings. The errno value is an index into the message string array and is automatically set to the index for the error generated. The number of entries in the array is determined by another constant, -_sys_nerr, also defined in STDLIB.H.
The function perror( ) has only one parameter, a character string. Normally, the argument passed is a string representing the file or function that generated the error condition. The following example demonstrates the simplicity of the function:
/* * perror.c * A C program demonstrating the function perror( ) * prototyped in stdio.h * Copyright (c) Chris H. Pappas and William H. Murray, 1998 */