C Library Macro -errno

C Standard Library - <errno.h>

Description

C Library Macroextern int errnois set by system calls, and certain library functions in error events indicate what error occurred.

errnois a macro in the C standard library, defined in<errno.h>In the header file. It is used to indicate errors that occur during program execution.errnois actually an integer variable used to store error codes. When library functions encounter an error, they seterrnoto an appropriate error code so that the program can inspect and handle these errors.

Declaration

Below is the declaration of the errno macro.

extern int errno

Parameter

  • NA

Return Value

  • NA

UsageerrnoSteps

  1. Include Header Files: When usingerrnoBefore, it needs to include<errno.h>Header file.
  2. Call Function: the call may seterrnoof the function.
  3. Checkserrno: when the function returns indicating an error, checkerrnoto get the error type.
  4. handle errors: According toerrnovalue, and take appropriate error-handling measures.

Example

The following example demonstrates the usage of the errno macro.

Example

#include <stdio.h> #include <errno.h> #include <string.h> extern int errno ; int main () { FILE *fp; fp = fopen("file.txt", "r"); if( fp == NULL ) { fprintf(stderr, "Value of errno: %d\n", errno); fprintf(stderr, "Error opening file: %s\n", strerror(errno)); } else { fclose(fp); } return(0); }

Let us compile and run the above program, when the filefile.txtWhen it does not exist, the following results will occur:

Value of errno: 2
Error opening file: No such file or directory

CommonerrnoValue

  • 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

Notes

  1. Thread Safety: In a multi-threaded environment,errnois usually implemented as thread-local storage (TLS), ensuring that each thread has an independenterrnovalue.
  2. Initial value: When the function call succeeds,errnois usually not modified. Therefore, before handling errors, you should ensureerrnois set to 0.
  3. Cross-platform differences: Different 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 suggestions

  1. Check the return value: When calling functions that may fail, always check their return value.
  2. Settingserrnois 0: Before calling the function, you canerrnoset to 0, so that after the function call you can checkerrnowhether it has been modified.
  3. Usagestrerrorandperror:strerrorThe function willerrnoconvert into a readable error message string;perrorfunction prints out the errno value with a descriptive error message.errnovalue.

Detailed error handling

Below is a more detailed example demonstrating how to handle differenterrnoValue:

Example

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

int main() {
    FILE *file = fopen("nonexistent_file.txt", "r");
    if (file == NULL) {
        switch (errno) {
            case EACCES:
                printf("Error: Permission denied\n");
                break;
            case ENOENT:
                printf("Error: No such file or directory\n");
                break;
            default:
                printf("Error opening file: %s\n", strerror(errno));
        }
        return 1;
    }

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

In this example, we checked the specificerrnovalue and handled accordingly. IferrnoYesEACCES(Permission denied) orENOENT(No such file or directory), we print a specific error message; otherwise, we print a generic error message.

<errno.h>provides a standardized way to report and handle errors in programs, making error handling code more consistent and maintainable.

C Standard Library - <errno.h>

other extensions