Suppose I have a function like the following:
#define LOWER_BOUND 0
#define UPPER_BOUND 42
int is_value_in_range( some_typedef val)
{
return ((LOWER_BOU
If some_typedef
is not known to be unsigned or signed, I think you're pretty much out of luck.
If you know in advance that some_typedef
is unsigned, you could just use
#if LOWER_BOUND > 0
return ((LOWER_BOUND <= val) && (val <= UPPER_BOUND));
#else
return ((val <= UPPER_BOUND));
#endif
Or in this case, you could use my preferred version:
return (val-LOWER_BOUND <= UPPER_BOUND-LOWER_BOUND);
Edit: I'm going to assume if some_typedef
is not known to be of particular signedness, then UPPER_BOUND
and LOWER_BOUND
must both be positive. Otherwise you would have very wacky results from some_typedef
getting promoted to unsigned. Thus, you could always safely use:
return ((uintmax_t)val-LOWER_BOUND <= UPPER_BOUND-LOWER_BOUND);