#include "stdio.h"
int main()
{
int x = -13701;
unsigned int y = 3;
signed short z = x / y;
printf("z = %d\n", z);
return 0;
}
I would expect the answer to be -4567. I am getting “z = 17278”.
Why does a promotion of these numbers result in 17278?
I executed this in Code Pad.
The hidden type conversions are:
When you mix signed and unsigned types the unsigned ones win.
xis converted tounsigned int, divided by 3, and then that result is down-converted to (signed)short. With 32-bit integers:By the way, the
signedkeyword is unnecessary.signed shortis a longer way of sayingshort. The only type that needs an explicitsignedischar.charcan be signed or unsigned depending on the platform; all other types are always signed by default.