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