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

C Standard Library –

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:

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

  1. Thread Safety: in multithreaded programs,errno is usually implemented as thread-local storage (Thread-Local Storage, TLS) to ensure that each thread has an independent error code.
  2. Initial value: On a successful call, library functions usually do not modify errno the value of errno. Therefore, before checking for errors, you should ensure that errno is set to 0.
  3. 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

  1. Check the return value: Always check the return value when calling a function that may fail.
  2. Setting errno to 0: Before calling a function, you can set errno errno to 0 so that after the call you can check errno has been modified.
  3. Use strerror: Through strerror data to the child component via errno converts 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.