strerror - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# strerror

From cppreference.com
< c‎ | string‎ | byte

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.

### [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

Run this code

    #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

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=c/string/byte/strerror&oldid=49106"