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
- Include Header Files: When using
errnoBefore, it needs to include<errno.h>Header file. - Call Function: the call may set
errnoof the function. - Checks
errno: when the function returns indicating an error, checkerrnoto get the error type. - handle errors: According to
errnovalue, and take appropriate error-handling measures.
Example
The following example demonstrates the usage of the errno macro.
Example
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 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
Notes
- Thread Safety: In a multi-threaded environment,
errnois usually implemented as thread-local storage (TLS), ensuring that each thread has an independenterrnovalue. - Initial value: When the function call succeeds,
errnois usually not modified. Therefore, before handling errors, you should ensureerrnois set to 0. - 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
- Check the return value: When calling functions that may fail, always check their return value.
- Settings
errnois 0: Before calling the function, you canerrnoset to 0, so that after the function call you can checkerrnowhether it has been modified. - Usage
strerrorandperror: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 <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.