5. strerror
char *strerror( int errnum);strerror returns a pointer to an implementation-defined string corresponding to error `errnum'.
On some Unix systems, strerror is implemented using an internal array of strings:
int sys_nerr; /* number of error messages */ char *sys_errlist[]; /* array of error messages */If 0 <= errnum < sys_nerr, the strerror return value is sys_errlist[ errnum]; otherwise it may return NULL or some string, and may or may not set errno depending on the system.
The standard library function perror, which prints an error message corresponding to the most recent I/O or system error to stderr, can be written using strerror:
#include <stdio.h> #include <string.h> #include <errno.h> /* something like: extern int errno; */ void perror( const char *s) { char *p = strerror( errno); fprintf( stderr, "%s: %s\n", s, (p == NULL) ? "" : p); }