C Library Function - strerror()
Description
C Library Function **char *strerror(int errnum) ** searches the internal array for the error number errnum, and returns a pointer to the error message string.strerror The generated error string depends on the development platform and compiler.
Declaration
Below is the declaration of the strerror() function.
char *strerror(int errnum)
Parameters
- errnum -- the error number, usually errno.
Return Value
This function returns a pointer to an error string that describes the error errnum.
Example
The following example demonstrates the use of the strerror() function.
#include
#include
#include
int main ()
{
FILE *fp;
fp = fopen("file.txt","r");
if( fp == NULL )
{
printf("Error: %s\n", strerror(errno));
}
return(0);
}
Let's compile and run the above program, which will produce the following result because we are trying to open a file that does not exist:
Error: No such file or directory
C Standard Library -