Using stdint.h
from glibc (gcc SUSE Linux version 9.2.1, Intel Core I7 processor) I came across a most strange behaviour when printing INT32_MIN
directly:
#include <stdio.h>
#include <stdint.h>
void main(void)
{
printf("%d\n", INT16_MIN);
int a = INT16_MIN;
printf("%d\n", a);
printf("%ld\n", INT32_MIN);
long b = INT32_MIN;
printf("%ld\n", b);
printf("%ld\n", INT64_MIN);
long c = INT64_MIN;
printf("%ld\n", c);
}
which outputs:
-32768
-32768
2147483648
-2147483648
-9223372036854775808
-9223372036854775808
Furthermore, if I try
printf("%ld\n", -INT32_MIN);
I get the same result, but with compiler warning: integer overflow in expression '-2147483648' of type 'int' results in '-2147483648' [-Woverflow]
.
Not that this is incredibly bad for any existing program, actually it seems pretty harmless, but is this a bug in good old printf?