errno is a preprocessor macro used for error indication.
- The value of errno is set to zero at program startup, and any function of the standard C++ library are allowed to write positive integers to errno whether or not an error occurred.
- Once the value of errno is changed from zero to non zero then no other function in the C++ standard library can change its value to zero.The errno is defined in cerrno heder file.
- The value of errno is set to 33 when there is an error in mathematical argument. In C++ error of mathematical argument is represented by EDOM whose value is 33.
The same header that declares errno () also declares at least the following macro constants with values different from zero:
- EDOM – Domain error: Some mathematical functions are only defined for certain real values, which is called its domain, for example, the square root and log function is only defined for non-negative numbers so if we pass negative argument in these function they set errno to EDOM
- ERANGE – Range error: The range of values that can be represented by a variable is limited. For example, mathematical functions such as pow can easily outbound the range representable by a floating point variable, or functions such as strtod can encounter sequences of digits longer than the range representable values. In these cases, errno is set to ERANGE.
- EILSEQ – Illegal sequence: Multibyte character sequence may have a restricted set of valid sequences. When a set of multibyte characters is translated by functions such as mbrtowc, errno is set to EILSEQ when an invalid sequence is encountered.
Below are the programs to implement the working of errno:
Program 1: This program detect error when negative value is passed in log function.
Value of errno is : 33 log(-1) is not valid : Numerical argument out of domain
Program 2: This program detect error when negative value is passed in square root function.
Value of errno is : 33 -100 is not valid argument for square root function : Numerical argument out of domain
Program 3: This program set errno to ERANGE.
Log(5) = 1.60944 Log(10) = 2.30259 Log(0) is out of range
- Different ways to declare variable as constant in C and C++
- Modulo Operator (%) in C/C++ with Examples
- fpclassify() method in C/C++ with Examples
- Operator Overloading '<<' and '>>' operator in a linked list class
- How to find the Entry with largest Value in a C++ Map
- Visibility Modes in C++ with Examples
- Speed up Code executions with help of Pragma in C/C++
- Vector of Vectors in C++ STL with Examples
- Array of Vectors in C++ STL
- How to call some function before main() function in C++?
- Difference between Virtual function and Pure virtual function in C++
- cin get() in C++ with Examples
- Difference between Increment and Decrement Operators
- Map of Vectors in C++ STL with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.