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.
The %d
conversion specifier in the format string of printf
converts the corresponding argument to a signed decimal integer, which in this case, overflows for the int
type. C standard specifically mentions that signed integer overflow is undefined behaviour. What you should do is to use %u
in the format string. Also, you need to include the headers stdio.h
and stdlib.h
for the prototype of the functions printf
and abs
respectively.
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
// This solves the issue of using the standard abs() function
unsigned int absu(int value) {
return (value < 0) ? -((unsigned int)value) : (unsigned int)value;
}
int main(void) {
printf("INT_MAX: %d\n", INT_MAX);
printf("INT_MIN: %d\n", INT_MIN);
printf("absu(INT_MIN): %u\n", absu(INT_MIN));
return 0;
}
This gives the output on my 32-bit machine:
INT_MAX: 2147483647
INT_MIN: -2147483648
absu(INT_MIN): 2147483648