C Library Macros - errno
Description
C Library Macros extern int errno is set by system calls and indicates what went wrong when an error event occurs in certain library functions.
errno is a macro in the C standard library, defined in header. It is used to indicate errors that occur during program execution.errno is actually an integer variable that stores an error code. When a library function encounters an error, it sets errno to an appropriate error code so the program can check and handle these errors.
Declaration
The following is the declaration of the errno macro.
extern int errno
Parameters
- NA
Return Value
- NA
Use errno Steps for
- Including the header: When using
errnoBefore that, you need to includeheader file in your C++ program. - Calling a function: calling may set
errno. - Checks
errno: when a function returns indicating an error, checkerrnoto obtain the error type. - Handling errors: based on the
errnovalue, take appropriate error-handling measures.
Example
The following example demonstrates the use 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's compile and run the above program; when the file file.txt does not exist, the following result is produced:
Value of errno: 2
Error opening file: No such file or directory
Common errno Value
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
Notes
- Thread Safety: in a multithreaded environment,
errnois usually implemented as Thread-Local Storage (TLS), ensuring each thread has an independenterrnovalues. - Initial value: when a function call succeeds,
errnois usually not modified. Therefore, before handling an error, make sureerrnois 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 recommendations
- Check the return value: always check the return value when calling a function that may fail.
- Settings
errnoto 0: before calling a function, you can seterrnoto 0 so that after the call you can check whethererrnohas been modified. - Use
strerrorandperror:strerrorfunction to installerrnoconvert it into a human-readable error message string;perrorfunction prints theerrnovalues.
Detailed error handling
The following is a more detailed example demonstrating how to handle different errno value:
Example
#include
#include
#include
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 handling code
fclose
(
file
)
;
return
0
;
}
In this example, we check the specific errno value and handle it accordingly. If errno Is EACCES(permission denied) or ENOENT(no such file or directory), we print specific error messages; otherwise, we print a generic error message.
provides a standardized way to report and handle errors in a program, making error-handling code more consistent and maintainable.
C Standard Library -