C Standard Library -<errno.h>

Introduction

of the C Standard Libraryerrno.hThe header file defines integer variableserrnoIt is set through system calls; in error events, certain library functions indicate what error occurred. This macro expands to a modifiable lvalue of type int, so it can be read and modified by a program.

<errno.h>is a header file in the C standard library that provides a mechanism for reporting and handling errors in programs. This header file defines macros and variables to indicate and describe the types of runtime errors.

errno.hThe header file defines a series of macros representing different error codes; these macros should expand to expressions of typeintinteger constant expression.

errnoIt is a global variable used to store the most recent error code. The type of this variable isintWhen a library function encounters an error, it usually setserrnoto indicate the type of error.

For example, in file operations, iffopenWhen a function fails because the file does not exist, it will seterrnoSet toENOENT。

Common Error Codes

The following are some common error codes; they are<errno.h>defined as macros in:

  • EPERM: Operation not permitted
  • ENOENT: No such file or directory
  • ESRCH: No such process
  • EINTR: Interrupted system call
  • EIO: Input/output error
  • ENXIO: No such device or address
  • E2BIG: Argument list too long
  • ENOMEM: Out of memory
  • EACCES: Permission denied
  • EFAULT: Bad address
  • EBUSY: Resource busy
  • EEXIST: File exists
  • EXDEV: Cross-device link
  • ENODEV: No such device
  • ENOTDIR: Not a directory
  • EISDIR: Is a directory
  • EINVAL: Invalid argument
  • ENFILE: System file table overflow
  • EMFILE: Too many open files
  • ENOTTY: Not a terminal device
  • ETXTBSY: Text file busy
  • EFBIG: File too large
  • ENOSPC: No space left on device
  • ESPIPE: Illegal addressing
  • EROFS: Read-only file system
  • EMLINK: Too many links
  • EPIPE: Broken pipe

Usage examples

The following is a simple example using errno, demonstrating how to handle file open errors:

Example

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

int main() {
    FILE *file = fopen("nonexistent_file.txt", "r");
    if (file == NULL) {
        printf("Error opening file: %s\n", strerror(errno));
        return 1;
    }

    // File processing code
    fclose(file);
    return 0;
}

In this example, iffopenFunction fails,errnowill be set to the corresponding error code.strerrorThe function is used toerrnoconverted to a human-readable error message.

Notes

  1. Thread Safety: In multi-threaded programs,errnoIt is usually implemented as thread-local storage (Thread-Local Storage, TLS) to ensure that each thread has an independent error code.
  2. Initial valueIn the case of a successful call, library functions usually do not modifyerrnothe value. Therefore, before checking the error, you should ensureerrnois set to 0.
  3. Error Code RangeDifferent operating systems and C standard library implementations may define additional error codes. Programs should avoid relying on the numeric values of specific error codes.

Error handling recommendations

  1. Check the return valueWhen calling a function that may fail, always check its return value.
  2. Settingerrnois 0: Before calling a function, you can seterrnoset to 0 so that it can be checked after the call.errnowhether it has been modified.
  3. Usagestrerror: throughstrerrorwillerrnoConvert it to a readable string to facilitate debugging and logging.

In short,<errno.h>It provides a standardized way to report and handle errors in a program, making error-handling code more consistent and maintainable.

Library macros

The following lists the macros defined in the header file errno.h:

Serial NumberMacro & Description
1extern int errno
This is a macro set through system calls; in error events, certain library functions indicate what error occurred.
2EDOM Domain Error
This macro represents a domain error, which occurs when the input argument is outside the domain defined by the mathematical function; errno is set to EDOM.
3ERANGE Range Error
This macro represents a range error, which occurs when the input argument exceeds the range defined by the mathematical function; errno is set to ERANGE.
other extensions