YouTip LogoYouTip

C Macro Erange

[![Image 1: C Standard Library - ](#) C Standard Library - ](#) ## Description The C library macro **ERANGE** indicates a range error, which occurs when an input argument exceeds the domain defined for a mathematical function; in such cases, `errno` is set to `ERANGE`. `ERANGE` is a macro defined in the C standard library header ``. It is used to indicate errors where the result falls outside the representable range of the functionβ€”commonly occurring when mathematical or conversion functions produce results that exceed what can be represented. ## Declaration Below is the declaration of the `ERANGE` macro. #define ERANGE some_value ## Parameters * **NA** ## Return Value * **NA** ## Example Below are several examples demonstrating the use of `ERANGE` to handle errors arising from results exceeding representable ranges. ### Mathematical Functions Range errors (`ERANGE`) may occur with mathematical functions such as `exp` or `log`. For instance, computing an extremely large exponent may yield a result beyond the floating-point representation range. ## Example #include #include #include int main(){ double x =1000.0; double result; // Clear any previous error errno=0; // Call exp(), which may result in overflow result =exp(x); if(errno==ERANGE){ printf("Error: exp() result out of range for input value %fn", x); }else{ printf("The exp of %f is %fn", x, result); } return 0; } In this example, computing `exp(1000.0)` may cause overflow, resulting in `errno` being set to `ERANGE`. Compiling and running the above program yields the following output: The exp of 1000.000000 is inf ### Conversion Functions During numeric conversionsβ€”for example, using `strtol` or `strtod`β€”if the converted result exceeds the range of the target type, `errno` is also set to `ERANGE`. ## Example #include #include #include #include int main(){ const char*str ="99999999999999999999999999"; long result; // Clear any previous error errno=0; // Call strtol(), which may result in overflow result =strtol(str, NULL, 10); if(errno==ERANGE){ printf("Error: strtol() result out of range for input string %sn", str); }else{ printf("The converted long value is %ldn", result); } return 0; } In this example, converting the string `"99999999999999999999999999"` to a `long` exceeds the representable range of `long`, causing `errno` to be set to `ERANGE`. Compiling and running the above program yields the following output: Error: strtol() result out of range for input string 99999999999999999999999999 ### Usage Notes 1. **Check return values**: After calling functions potentially subject to range errors, inspect their return valuesβ€”such functions typically return special values (e.g., `HUGE_VAL`, `HUGE_VALF`, `HUGE_VALL`, or `0`) upon error. 2. **Initialize `errno`**: Before invoking a function, set `errno` to `0` to ensure the error code reflects only the current callβ€”not residual state from prior invocations. 3. **Handle range errors appropriately**: Upon detecting an `ERANGE` error, respond contextuallyβ€”for example, by printing an error message, substituting a fallback value, or retrying the computation. ### Related Functions and Error Codes * **Mathematical functions**: e.g., `exp`, `log`, `pow`, `sin`, `cos`, `tan`; these may set `errno` to `ERANGE` when computed results exceed representable limits. * **Conversion functions**: e.g., `strtol`, `strtoll`, `strtoul`, `strtoull`, `strtof`, `strtod`, `strtold`; these may set `errno` to `ERANGE` when converted values exceed representable limits. #### Other Related Error Codes * `EDOM`: Indicates a domain error in mathematical functionsβ€”i.e., an argument lies outside the function’s domain. * `EILSEQ`: Indicates an illegal byte sequence error, commonly encountered during multibyte or wide-character processing. By using `errno`, `ERANGE`, and related functions appropriately, you can write more robust and reliable code, ensuring proper handling and response under various exceptional conditions. [![Image 2: C Standard Library - ](#) C Standard Library - ](#)
← C Function SetlocaleC Macro Edom β†’