I thought the following code might cause an overflow since a * 65535
is larger than what unsigned short int
can hold, but the result seems to be correct.
Is there some built-in mechanism in C that stores intermediary arithmetic results in a larger data type? Or is it working as an accident?
unsigned char a = 30;
unsigned short int b = a * 65535 /100;
printf("%hu", b);
It works because all types narrower than
int
will go under default promotion. Sinceunsigned char
andunsigned short
are both smaller than int on your platform, they'll be promoted toint
and the result won't overflow ifint
contains22 bits or more (which is the number of bits inCHAR_BIT + 17
bits or more (which is the result ofa * 65535
plus sign)30 * 65535
plus sign). However ifint
has fewer bits then overflow will occur and undefined behavior happens. It won't work ifsizeof(unsigned short) == sizeof(int)
eitherDefault promotion allows operations to be done faster (because most CPUs work best with values in its native size) and also prevents some naive overflow from happening. See