How could I extract the absolute value of INT_MIN
without overflowing? See this code for the problem:
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {
printf("INT_MAX: %d\n", INT_MAX);
printf("INT_MIN: %d\n", INT_MIN);
printf("abs(INT_MIN): %d\n", abs(INT_MIN));
return 0;
}
Spits out the following
INT_MAX: 2147483647
INT_MIN: -2147483648
abs(INT_MIN): -2147483648
I need this for a check if an int
value is greater than zero.
As for this question being a duplicate of Why the absolute value of the max negative integer -2147483648 is still -2147483648?, I have to disagree, since this is a HOW, not a WHY question.