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

C Library Macros – ERANGE

C Library Macros - ERANGE

C Standard Library - <errno.h> C Standard Library -

Description

C Library Macros ERANGE represents a range error, which occurs when an input argument is outside the range defined by a mathematical function; errno is set to ERANGE.

ERANGE is a macro in the C standard library, defined in header. It is used to indicate an error where the result is outside the function's defined range, usually occurring when the computation result of a math or conversion function exceeds the representable range.

Declaration

The following is the declaration of the ERANGE macro.



#define ERANGE some_value

Parameters

Return Value

Example

The following are a few examples using ERANGE that demonstrate how to handle result-out-of-range errors.

Mathematical functions

In mathematical computations, such as exp or log functions, a ERANGE error may occur. For example, when computing a very large exponent, the result may exceed the representable range of a floating-point number.

Example

#include

#include

#include

int

main

(

)

{

   

double

x

=

1000.0

;

   

double

result

;

   

// Clear previous errors

   

errno

=

0

;

   

// Call the exp function; the result may exceed the representable range

    result

=

exp

(

x

)

;

   

if

(

errno

==

ERANGE

)

{

       

printf

(

"Error: exp() result out of range for input value %f\n"

, x

)

;

   

}

else

{

       

printf

(

"The exp of %f is %f\n"

, x, result

)

;

   

}

   

return

0

;

}

In this example, computing exp(1000.0) may cause the result to exceed the range, in which case errno is set to ERANGE.

Let's compile and run the above program, which will produce the following result:

The exp of 1000.000000 is inf

Conversion functions

In numeric conversions, such as strtol or strtod functions, if the conversion 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 previous errors

   

errno

=

0

;

   

// Call the strtol function; the result may exceed the representable range

    result

=

strtol

(

str,

NULL

,

10

)

;

   

if

(

errno

==

ERANGE

)

{

       

printf

(

"Error: strtol() result out of range for input string %s\n"

, str

)

;

   

}

else

{

       

printf

(

"The converted long value is %ld\n"

, result

)

;

   

}

   

return

0

;

}

In this example, the string "99999999999999999999999999" Converts to long numeric value, the result exceeds the long representable range oferrno is set to ERANGE.

Let's compile and run the above program, which will produce the following result:

Error: strtol() result out of range for input string 99999999999999999999999999

Usage notes

  1. Check the return value: after calling a function that may produce a range error, check the return value; typically these functions return a special value on error (such as HUGE_VAL, HUGE_VALF, HUGE_VALL or 0).
  2. Initialization errno: before calling the function, set errno to 0 to ensure the error code comes from this call and not a leftover from a previous one.
  3. Handling range errors: when an ERANGE error is detected, it should be handled appropriately according to the context, such as printing an error message, using an alternative value, or retrying the computation.

Related functions and error codes

Other related error codes

By using properly errno, ERANGE and its related functions, more robust and reliable code can be written, ensuring the program still handles and responds normally under various exceptional conditions.

C Standard Library - <errno.h> C Standard Library -