Reputation: 13
Why is it possible to store -32 768 as a short int?
printf("%hd", -32768);
//returns -32768, no compiler errors
printf("%hd", (short int)-32768);
//returns -32768, no compiler errors
short int a = -32768;
printf("%hd", a);
//returns -32768, no compiler errors
printf("%d", -2147483648);
//compiler returned error as it should
And why doesn't short int overflow throw an error in the compiler?
printf("%hd", -32769);
//returns 32767, no compiler errors
while
printf("%d", -2147483648);
//compiler returned error
I am using 64 bit ubuntu 16.04 with the gcc compiler version 5.4.0. It has something to do with the printf function? or I don't understand something.
Upvotes: 0
Views: 420
Reputation: 76428
There's nothing magic about passing those values to printf
. Remember, its prototype is int printf(const char*, ...)
. When a numeric argument that's smaller than int
is passed through an ellipsis (the ...
at the end of the argument list) it gets promoted to int
. So printf
expects all values whose types are smaller than int
to be passed as int
.
However, in this case, there are no integral promotions involved. The type of -32768
is int
. Same for -32769
. It's up to the code inside printf
to apply the format specifier (here, "%hd"
) and try to make sense out of the values that are passed to it. This code passes integer values and claims that their type is short
. It looks like it works, but, in general, don't expect sense from that.
Concerning (short)-32768
, if that value fits in a short (which it probably does; check SHORT_MIN
to find out), no problem. If not, the result of the conversion is implementation-defined; it's not illegal.
And, finally, -32769
has type int
; there's no overflow, and the result that you're seeing is just the formatting code trying to make sense out of the bit pattern, on the assumption that the value it sees is the result of promoting a short
to int
. Again, don't expect sense from that.
Upvotes: 1