This is crucial since it determines the stability, reliability, and robustness of the final program that we will compile.
For example, the fopen() function returns “0” if it could not open the specified file correctly for some reason. However, in functions such as math, a numeric value of “0” might represent a correct result. This type of return only reports an error but cannot specify which one it is.
The standard library of the C language provides a variety of predefined error codes in the “errno.h” header.
These error codes are specific and indicates the origin or cause of the error. Therefore, they are a very handy tool when it comes to adding robustness to our program.
In this Linux Hint article, we will explain the ERANGE error code which indicates the values out of range. We will look at the causes that cause this error and how to fix them. Then, we generate this error in the code fragments that we will accompany with images that show its detection and possible solutions.
Error Code ERANGE: Definition and Numerical Representation
Error Code ERANGE Description
The error code ERANGE represents an out-of-range exception that typically occurs when a function returns a value that is too large to exceed the size of a long. This error code is represented in gcc with the integer value of 34.y which is predefined in the “errno.h” header.
If one of these errors occurs in a mathematical function, the function returns an infinite result or -inf. And the numeric representation of the ERANGE error code is automatically stored in the “errno” variable of external int type which is predefined in the “errno.h” file.
The “errno” variable stores the numeric code of the last error. Each time we retrieve the value that is stored in it, we have to clean its content.
This and all other standard library error codes are defined in the “errno.h” header file. To use these codes, we need to insert this header into our code as follows:
Once the “errno.h” header is defined in our code, we can use this resource to query and classify the various errors that may occur during the process.
How to Detect an Out-of-Range Error with the “Errno” Variable and the ERANGE Code in the C Language
In this example, we will create an out-of-range error and show how you can detect and classify it by retrieving the information from the predefined “errno” variable in the “errno.h” header.
We generate the error by trying to calculate the logarithm of 0 using the log() function.
When we try to get the logarithm of 0, this function returns “-inf” as a result and generates the error code – Out-of-Range or ERANGE.
You can see the code fragment in which we try to get the logarithm of zero in the following. The result displayed in the command console:
The following image that we see shows the result of the execution of this fragment:
The following snippet performs the same operation but prints the contents of the “errno” variable to the command console with the error number which is generated when trying to get the logarithm of 0:
As we can see in the following figure, the “errno” variable contains the integer 34 which is defined in the “errno.h” header for the ERANGE error or range error:
As we have seen in this example, both ERANGE and any other error code that are defined in the “errno.h” header can be queried via the “errno” variable.
It is important to clean up this variable as soon as we retrieve the data from it, as this can lead to confusion or incorrect error interpretations. To do this, we set it to zero as shown in the next line of code:
Conclusion
In this Linux Hint article about the C language, we learned the meaning of the ERANGE error code which is part of the standard library code set which is defined in the “errno.h” header. We also showed you why this error is generated, its numerical representation, and the “errno” variable that is used to process and store this code. We hope that this article has been useful. For more articles about the C language, use the search engine on our website.