std::strerror

Header: <cstring>

Returns a pointer to the textual description of the system error code errnum, identical to the description that would be printed by std::perror().

# Declarations

char* strerror( int errnum );

# Parameters

# Return value

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

# Notes

POSIX allows subsequent calls to strerror to invalidate the pointer value returned by an earlier call. It also specifies that it is the LC_MESSAGES locale facet that controls the contents of these messages.

POSIX has a thread-safe version called strerror_r defined. Glibc defines an incompatible version.

# Example

#include <cerrno>
#include <clocale>
#include <cmath>
#include <cstring>
#include <iostream>
 
int main()
{
    const double not_a_number = std::log(-1.0);
    std::cout << not_a_number << '\n';
 
    if (errno == EDOM)
    {
        std::cout << "log(-1) failed: " << std::strerror(errno) << '\n';
        std::setlocale(LC_MESSAGES, "de_DE.utf8");
        std::cout << "Or, in German, " << std::strerror(errno) << '\n';
    }
}

# See also