errno.h in C

The file header named errno.h in Standard C library defines the integer variable errno, which is set by system call and some library functions for errors to indicate that an error is occurring. This macro is an extension of the type of lvalue type int which can be modified, so it can be read and modified by a program.

errno.h in C

The file header named errno.h in Standard C library defines the integer variable errno, which is set by system call and some library functions for errors to indicate that an error is occurring. This macro is an extension of the type of lvalue type int which can be modified, so it can be read and modified by a program.

errno is set to 0 at the program start. The specific functions of the standard C library modify its value to a number greater than 0 to signal some type of error. You can also modify its value or restore it to 0 depending on your requirements.

The file header named errno.h also defines a list of macros to indicate different error codes.

The macros are defined in errno.h

The table below lists some macros defined in errno.h in Library C:

Macro Descriptionextern int errno

This is a macro set by system call and some library functions for errors to indicate that an error is occurring

EDOM Domain Error

This macro represents a domain error, which occurs if an input parameter is outside the domain where the math function is defined and errno is set to EDOM.

ERANGE Range Lỗi

This macro represents a range error, which occurs if an input parameter is outside the range of values ​​for which the function is defined, and errno is set to ERANGE.

1. Macro errno in C

Macro errno in C

The extern int errno macro in Library C is set by system call and some library functions for errors indicate that an error is occurring.

Declaring errno Macro in C

Below is the declaration for errno macro in C.

 extern int errno 

Parameters

This macro does not receive any parameters

Returns the value

This macro does not return any values

For example

The following C program illustrates the usage of errno Macro in C.

 #include #include #include extern int errno ; int main () { FILE * fp ; fp = fopen ( "fileexample.txt" , "r" ); if ( fp == NULL ) { fprintf ( stderr , "Gia tri cua errno: %dn" , errno ); fprintf ( stderr , "Xay la loi khi mo file: %sn" , strerror ( errno )); } else { fclose ( fp ); } return ( 0 ); } 

Compile and run the above C program in case the file.txt does not exist:

errno.h in C Picture 1errno.h in C Picture 1

2. EDOM macro in C

EDOM macro in C

EDOM macros in Library C represent a domain error, which occurs if an input parameter is outside the domain where the math function is defined and errno is set to EDOM.

EDOM Macro declaration in C

Below is the declaration for EDOM Macro in C.

 #define EDOM some_value 

Parameters

This macro does not receive any parameters

Returns the value

This macro does not return any values

For example

Program C follows the usage of EDOM Macro.

 #include #include #include int main () { double val ; errno = 0 ; val = sqrt (- 10 ); if ( errno == EDOM ) { printf ( "Gia tri khong hop le n" ); } else { printf ( "Gia tri hop len" ); } errno = 0 ; val = sqrt ( 10 ); if ( errno == EDOM ) { printf ( "Gia tri khong hop len" ); } else { printf ( "Gia tri hop len" ); } return ( 0 ); } 

Compiling and running the above C program will result:

errno.h in C Picture 2errno.h in C Picture 2

3. Macro ERANGE in C

Macro ERANGE in C

The Macro ERANGE in Library C represents a range error, which occurs if an input parameter is outside the range of values ​​for which the function is defined, and errno is set to ERANGE.

Declared Macro ERANGE in C

Below is the declaration for ERANGE Macro in C.

 #define ERANGE some_value 

Parameters

This macro does not receive any parameters

Returns the value

This macro does not return any values

For example

The following C program illustrates the use of ERANGE Macro.

 #include #include #include int main () { double x ; double value ; x = 2.000000 ; value = log ( x ); if ( errno == ERANGE ) { printf ( "Log(%f) la ben ngoai day gia tri cua ham logn" , x ); } else { printf ( "Log(%f) = %fn" , x , value ); } x = 1.000000 ; value = log ( x ); if ( errno == ERANGE ) { printf ( "Log(%f) la ben ngoai day gia tri cua ham logn" , x ); } else { printf ( "Log(%f) = %fn" , x , value ); } x = 0.000000 ; value = log ( x ); if ( errno == ERANGE ) { printf ( "Log(%f) la ben ngoai day gia tri cua ham logn" , x ); } else { printf ( "Log(%f) = %fn" , x , value ); } return 0 ; } 

Compiling and running the above C program will result:

errno.h in C Picture 3errno.h in C Picture 3

According to Tutorialspoint

Last post: ctype.h in C

Next lesson: float.h in C

4.5 ★ | 4 Vote