Namespaces
Variants
Views
Actions

strerror

From cppreference.com
< c‎ | string‎ | byte
Revision as of 17:41, 31 May 2013 by P12bot (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Defined in header <string.h>
char* strerror( int errnum );

Returns text version of the error code errnum. errnum is usually acquired from the errno variable, however the function accepts any value of type int. The message is locale-specific.

The returned byte string must not be modified by the program, but may be overwritten by a subsequent call to the strerror function.

Contents

[edit] Parameters

errnum - integral value referring to a error code

[edit] Return value

Pointer to a null-terminated byte string corresponding to the error code errnum.

[edit] Example

#include <stdio.h>
#include <errno.h>
#include <string.h>
 
int main(void)
{
    FILE *fp;
    char *path = "/path/to/non_existing_file";
 
    errno = 0;
    if ((fp = fopen(path, "r")) == NULL) {
        char *error_str = strerror(errno);
        printf("Can't open \"%s\": %s\n", path, error_str);
    }
    printf("Error string for EINVAL: %s\n", strerror(EINVAL));
 
    return 0;
}

Output:

Can't open "/path/to/non_existing_file": No such file or directory
Error string for EINVAL: Invalid argument

[edit] See also

C++ documentation for strerror