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 permittedENOENT: No such file or directoryESRCH: No such processEINTR: Interrupted system callEIO: Input/output errorENXIO: No such device or addressE2BIG: Argument list too longENOMEM: Out of memoryEACCES: Permission deniedEFAULT: Bad addressEBUSY: Resource busyEEXIST: File existsEXDEV: Cross-device linkENODEV: No such deviceENOTDIR: Not a directoryEISDIR: Is a directoryEINVAL: Invalid argumentENFILE: System file table overflowEMFILE: Too many open filesENOTTY: Not a terminal deviceETXTBSY: Text file busyEFBIG: File too largeENOSPC: No space left on deviceESPIPE: Illegal addressingEROFS: Read-only file systemEMLINK: Too many linksEPIPE: Broken pipe
Usage examples
The following is a simple example using errno, demonstrating how to handle file open errors:
Example
#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
- 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. - Initial valueIn the case of a successful call, library functions usually do not modify
errnothe value. Therefore, before checking the error, you should ensureerrnois set to 0. - 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
- Check the return valueWhen calling a function that may fail, always check its return value.
- Setting
errnois 0: Before calling a function, you can seterrnoset to 0 so that it can be checked after the call.errnowhether it has been modified. - Usage
strerror: 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 Number | Macro & Description |
|---|---|
| 1 | extern int errno This is a macro set through system calls; in error events, certain library functions indicate what error occurred. |
| 2 | EDOM 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. |
| 3 | ERANGE 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. |