How can I portably find out the smallest of INT_MAX and abs(INT_MIN)? (That\'s the mathematical absolute value of INT_MIN, not a call
abs(INT_MIN) will invoke undefined behavior. Standard says
abs, labs and llabs functions:The
abs,labs, andllabsfunctions compute the absolute value of an integerj. If the result cannot be represented, the behavior is undefined.
Try this instead :
Convert INT_MIN to unsignrd int. Since -ve numbers can't be represented as an unsigned int, INT_MAX will be converted to UINT_MAX + 1 + INT_MIN.
#include
#include
unsigned min(unsigned a, unsigned b)
{
return a < b ? a : b;
}
int main(void)
{
printf("%u\n", min(INT_MAX, INT_MIN));
}