C strerror() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
string.h

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 <string.h> and <errno.h>.

Syntax

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

C
strerror(errno);
CallWhat it does
strerror(errno)Looks up a short message for the current errno value.

Include <string.h> and <errno.h>. Exact wording can differ by OS and locale.

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
Message pointerPoints to library-owned text. Do not free it or change it.

C Examples

Example 1 — Look Up a Known Code

Pass an errno.h constant directly to see its message.

C
#include <stdio.h>
#include <string.h>
#include <errno.h>

int main(void) {
    printf("%s\n", strerror(ENOENT));
    return 0;
}

How It Works

  1. Include errno.h. ENOENT is a named error constant.
  2. Call strerror. The library maps that number to a message string.
  3. Print the text. You get a readable phrase instead of a raw integer.
  4. Wording may vary. Different systems can phrase the same error slightly differently.

Example 2 — Report a Failed fopen

After a failed open, print strerror(errno) so the user knows why it failed.

C
#include <stdio.h>
#include <string.h>
#include <errno.h>

int main(void) {
    FILE *fp = fopen("missing.txt", "r");

    if (fp == NULL) {
        printf("open failed: %s\n", strerror(errno));
        return 1;
    }

    fclose(fp);
    return 0;
}

How It Works

  1. fopen fails. Opening a missing file returns NULL.
  2. errno is set. The library stores a code such as ENOENT.
  3. strerror translates it. You print a clear message with your own prefix.
  4. Exit with status 1. Signals failure to the shell or caller.

Example 3 — Save errno Before Other Calls

Other library functions can change errno. Save it first, then format the message.

C
#include <stdio.h>
#include <string.h>
#include <errno.h>

int main(void) {
    FILE *fp = fopen("missing.txt", "r");

    if (fp == NULL) {
        int err = errno;
        printf("code = %d\n", err);
        printf("text = %s\n", strerror(err));
        return 1;
    }

    fclose(fp);
    return 0;
}

How It Works

  1. Capture errno. err = errno freezes the failure code.
  2. Print the number. Useful for logs and debugging (exact value can vary).
  3. Translate with strerror. Pass the saved err, not a later errno.
  4. Safe habit. This pattern stays correct even if you add more calls before printing.

Frequently Asked Questions

strerror() turns an error number into a short message you can print.
errnum is required. It is usually errno right after a failing call.
A pointer to library-owned text. Do not free it or change it.
Include <string.h> and <errno.h>.
Read it immediately after the failing call, before another library call replaces it.

Next: strpbrk()

Locate the first character in a string that belongs to a given set.

strpbrk() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

9 people found this page helpful