The strerror function returns a pointer to the error-message
STRERROR(S) UNIX System V STRERROR(S)
Name
strerror - gets error message pointer from last routine call
error
Syntax
#include <string.h>
char *strerror(errnum)
int errnum;
Description
The strerror routine maps errnum to an error-message string,
returning a pointer to the string. The function itself does
not actually print the message; for that, you need to call
an output function such as printf.
Return Value
The strerror function returns a pointer to the error-message
string. The string can be overwritten by subsequent calls
to strerror.
See Also
clearerr(S), ferror(S), perror(S)
Example
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
extern int errno;
int errnum;
int fh1, fh2;
main()
{
errnum=0;
if ((fh1=open("xxxx",O_RDONLY)) == -1)
errnum=errno;
fh2=open("yyyy",O_RDONLY);
/* Other code that may set the errno value.*/
if (errnum != 0)
printf(strerror(errnum));
}
The program shown above tries to open files xxxx and yyyy.
If an error occurs opening xxxx, the variable errnum is set
to the errno value returned by open. Other code that may
alter the errno value is then executed. Later, the saved
errno value in errnum is checked and, if nonzero, an error
message assigned to it by strerror is printed. If file xxxx
does not exist, the example will print the following
message:
No such file or directory
Standards Conformance
strerror is conformant with:
ANSI X3.159-198X C Language Draft Standard, May 13, 1988.
(printed 6/20/89)