C <errno.h> library


The <errno.h> header file in C is part of the C Standard Library and is used for error handling in C programs. It defines the errno variable, which is used to indicate error conditions in system calls and library functions. When an error occurs, these functions set errno to a specific error code, which can then be checked by the program to determine what went wrong.

Key Features of <errno.h>

  1. Error Handling: Provides a standardized way to report and handle errors across different functions and libraries.
  2. Error Codes: Defines a set of error codes that correspond to specific error conditions.
  3. Global Variable: The errno variable is typically defined as an int and is set by various functions when an error occurs.

Commonly Used Elements in <errno.h>

1. errno Variable

  • errno: A global variable defined in <errno.h>. It is used by various functions to indicate what error occurred.

    Example:

    #include <stdio.h> #include <errno.h> FILE *file = fopen("nonexistent.txt", "r"); if (file == NULL) { printf("Error opening file: %s\n", strerror(errno)); }

2. Error Codes

<errno.h> defines several error codes, which are set in the errno variable when an error occurs. Some of the most common error codes include:

  • ENOMEM: Insufficient memory to allocate.
  • EINVAL: Invalid argument passed to a function.
  • EIO: Input/output error.
  • ENOENT: No such file or directory.
  • EPERM: Operation not permitted.
  • EACCES: Permission denied.
  • EEXIST: File exists.

You can check the value of errno to determine the type of error that occurred after a function call fails.

Commonly Used Functions in <errno.h>

1. strerror()

  • strerror(): Returns a pointer to the textual representation of the current errno value.

    Syntax:

    char *strerror(int errnum);

    Example:

    #include <stdio.h> #include <string.h> #include <errno.h> int main() { FILE *file = fopen("nonexistent.txt", "r"); if (file == NULL) { printf("Error: %s\n", strerror(errno)); // Prints a human-readable error message } return 0; }

Important Notes

  1. Thread Safety: In a multi-threaded program, errno is not thread-safe because it is a global variable. Each thread should ideally use its own mechanism to handle errors (e.g., using thread-local storage).

  2. Resetting errno: It is a good practice to set errno to zero before calling a function that can fail, especially if you want to distinguish between a successful return and an error condition.

  3. Library Functions: Not all functions set errno on success or failure. It is essential to refer to the documentation of each function to understand its behavior regarding errno.

Example Usage

Here’s a simple example that demonstrates error handling with <errno.h>:

#include <stdio.h> #include <errno.h> #include <string.h> int main() { // Attempt to open a non-existent file FILE *file = fopen("nonexistent.txt", "r"); if (file == NULL) { // Check the error code if (errno == ENOENT) { printf("Error: File does not exist.\n"); } else { printf("Error: %s\n", strerror(errno)); } } else { // File opened successfully printf("File opened successfully.\n"); fclose(file); } return 0; }

Summary

  • <errno.h> provides a standardized way to report and handle errors in C programs.
  • The errno variable is set by various functions when an error occurs, and its value can be used to determine the specific error.
  • The strerror() function is useful for converting error codes into human-readable strings.
  • Understanding how to use <errno.h> is crucial for effective error handling in C programming.