C errno and perror()

Advanced
6 min read
Updated: Nov 2026
1 example

What Is errno and perror()?

errno (from <errno.h>) is an integer the library sets when a call fails. Set it to 0 before calls that do not report errors through their return value, then test it. perror("text") prints your text, a colon, and the message for the current errno to stderr; strerror(errno) returns that message as a string.

Remember
#include <errno.h>
errno = 0;
long v = strtol(s, &end, 10);
if (errno == ERANGE) { /* out of range */ }
perror("fopen");                  /* "fopen: No such file ..." */
fprintf(stderr, "%s\n", strerror(errno));

Cheat Sheet

Keep these facts handy while you work with errno and perror() in C.

TopicDetail
Header<errno.h>
ResetSet errno = 0 before the call
Common codesENOENT, EACCES, ENOMEM, ERANGE
Threaderrno is per-thread

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Assertions.

Example 1 — Detect an out-of-range conversion

strtol sets errno to ERANGE for numbers that do not fit in a long.

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

int main(void) {
    errno = 0;
    long v = strtol("99999999999999999999", NULL, 10);

    if (errno == ERANGE) {
        printf("out of range (ERANGE)\n");
    } else {
        printf("value = %ld\n", v);
    }
    return 0;
}

How It Works

  1. errno = 0 clears any earlier error.
  2. The 20-digit value exceeds LONG_MAX, so strtol returns LONG_MAX and sets ERANGE.
  3. Use perror when you want the message printed to stderr.

Edge Cases & Pitfalls

Check these before relying on errno and perror() in real programs.

stale

Reading errno after another call

Any later library call may change errno. Copy it immediately after the failure.

success

Assuming errno is 0 on success

Successful calls do not reset errno. Only check it after a call that signaled failure.

Key Takeaways

  • Reset: before the call if needed.
  • Check: only after failure.
  • Print: perror / strerror.

One line: errno (from <errno.h>) is an integer the library sets when a call fails.

Frequently Asked Questions

errno is a thread-local integer that library functions set to an error code when they fail.
perror prints a message directly to stderr. strerror returns the message text so you can format and send it anywhere.
Continue with "C Assertions" next. Catch programmer mistakes with assert() and disable them in release builds. The C Advanced Concepts hub lists every topic in order.

Next: Assertions

Catch programmer mistakes with assert() and disable them in release builds.

Continue →

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.

11 people found this page helpful