Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Library Macros – errno

C Library Macros - errno

C Standard Library - <errno.h> C Standard Library -

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

Return Value

Use errno Steps for

  1. Including the header: When using errno Before that, you need to include header file in your C++ program.
  2. Calling a function: calling may set errno .
  3. Checks errno: when a function returns indicating an error, check errno to obtain the error type.
  4. Handling errors: based on the errno value, 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

Notes

  1. Thread Safety: in a multithreaded environment,errno is usually implemented as Thread-Local Storage (TLS), ensuring each thread has an independent errno values.
  2. Initial value: when a function call succeeds,errno is usually not modified. Therefore, before handling an error, make sure errno is set to 0.
  3. 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

  1. Check the return value: always check the return value when calling a function that may fail.
  2. Settings errno to 0: before calling a function, you can set errno to 0 so that after the call you can check whether errno has been modified.
  3. Use strerror and perror:strerror function to install errno convert it into a human-readable error message string;perror function prints the errno values.

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 - <errno.h> C Standard Library -