C Standard Library -
Introduction
The C standard library's errno.h The header file defines the integer variable errno, which is set by system calls and certain library functions in the event of an error to indicate what went wrong. This macro expands to a modifiable lvalue of type int, so it can be read and modified by a program.
is a header file in the C standard library that provides a mechanism for reporting and handling errors in a program. This header file defines macros and variables used to indicate and describe the types of runtime errors.
errno.h The header file defines a series of macros representing different error codes, which should expand to integer constant expressions of type int int.
errno is a global variable used to store the most recent error code. This variable is of type intint. When a library function encounters an error, it usually sets errno to indicate the error type.
For example, in file operations, if the fopen function fails because the file does not exist, it sets errno to ENOENT.
Common error codes
Here are some common error codes, which are defined as macros in errno.h:
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: Not enough memoryEACCES: Permission deniedEFAULT: Bad addressEBUSY: Device or resource busyEEXIST: File existsEXDEV: Invalid cross-device linkENODEV: No such deviceENOTDIR: Not a directoryEISDIR: Is a directoryEINVAL: Invalid argumentENFILE: Too many open files in systemEMFILE: Too many open filesENOTTY: Not a typewriterETXTBSY: Text file busyEFBIG: File too largeENOSPC: No space left on deviceESPIPE: Illegal seekEROFS: Read-only file systemEMLINK: Too many linksEPIPE: Broken pipe
Usage examples
The following is a simple example using errno that demonstrates how to handle a file-open error:
Example
#include
#include
#include
int
main
(
)
{
FILE
file
=
fopen
(
"nonexistent_file.txt"
,
"r"
)
;
if
(
file
==
NULL
)
{
printf
(
"Error opening file: %s\n"
,
strerror
(
errno
)
)
;
return
1
;
}
// File handling code
fclose
(
file
)
;
return
0
;
}
In this example, if the fopen function fails,errno is set to the corresponding error code.strerror The function is used to convert errno it into a human-readable error message.
Notes
- Thread Safety: in multithreaded programs,
errnois usually implemented as thread-local storage (Thread-Local Storage, TLS) to ensure that each thread has an independent error code. - Initial value: On a successful call, library functions usually do not modify
errnothe value of errno. Therefore, before checking for errors, you should ensure thaterrnois set to 0. - Error code ranges: Different operating systems and C standard library implementations may define additional error codes. Programs should avoid relying on the specific numeric values of error codes.
Error handling recommendations
- Check the return value: Always check the return value when calling a function that may fail.
- Setting
errnoto 0: Before calling a function, you can seterrnoerrno to 0 so that after the call you can checkerrnohas been modified. - Use
strerror: Throughstrerrordata to the child component viaerrnoconverts it into a readable string, making debugging and logging easier.
In summary, provides a standardized way to report and handle errors in a program, making error-handling code more consistent and maintainable.
Library macros
The macros defined in the header file errno.h are listed below:
| No. | Macro & description |
|---|---|
| 1 | extern int errno |
| This is a macro set by system calls and certain library functions in the event of an error to indicate what went wrong. | |
| 2 | EDOM Domain Error |
| This macro represents a domain error, which occurs when an input argument is outside the domain defined by a mathematical function; errno is set to EDOM. | |
| 3 | ERANGE Range Error |
| This macro represents a range error, which occurs when an input argument is outside the range defined by a mathematical function; errno is set to ERANGE. |