C++ strerror() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cstring>

Definition

In C++, strerror() turns an error number into a short message you can print. You usually pass errno right after a library call fails.

The error number is required. The result is a pointer to text that the library owns, so do not free it or change it. Read errno before another call replaces it. Include <cstring> and <cerrno>.

Syntax

Pass the error number. That is usually errno just after a call fails.

C++
strerror(errno);
CallWhat it does
strerror(errno)Returns a message for the current error number.
Library declarations
C++
char* strerror(int errnum);

Parameters

ParameterDescription
errnumRequired. The error number, usually errno right after the call that failed.

Return Value

Return ValueDescription
MessageA pointer to text that describes the error. The library owns that text.

C++ Examples

Example 1 — Message for a Known Error Code

Look up ENOENT without opening a file.

C++
#include <iostream>
#include <cstring>
#include <cerrno>

int main() {
    std::cout << strerror(ENOENT) << '\n';
    return 0;
}

How It Works

  1. ENOENT is a standard “missing file” code.
  2. strerror maps that number to text.
  3. Wording may vary slightly by platform.

Example 2 — After a Failed File Open

Use errno set by a failing C I/O call.

C++
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cerrno>

int main() {
    FILE* fp = std::fopen("no_such_file.txt", "r");

    if (fp == nullptr) {
        std::cout << "Open failed: " << strerror(errno) << '\n';
        return 1;
    }

    std::fclose(fp);
    return 0;
}

How It Works

  1. fopen fails and sets errno.
  2. Check for nullptr immediately.
  3. Print strerror(errno) for a clear reason.

Example 3 — Copy the Message into std::string

Keep the text safe if you call strerror again later.

C++
#include <iostream>
#include <string>
#include <cstring>
#include <cerrno>

int main() {
    std::string msg = strerror(EINVAL);

    std::cout << "Saved: " << msg << '\n';
    std::cout << "Again: " << strerror(ENOENT) << '\n';
    std::cout << "Still: " << msg << '\n';
    return 0;
}

How It Works

  1. Constructing a std::string copies the characters.
  2. A second strerror call may reuse the library buffer.
  3. Your msg copy is unchanged.

Frequently Asked Questions

strerror() turns an error number into a short message you can print.
errnum is the error number. It is usually the current errno.
A pointer to a message. The library owns that text.
No. Do not free or change the pointer strerror() returns.
Right after the call that failed. A later library call can replace errno.

Next: strlen()

Count the characters in a null-terminated C-string.

strlen() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful