I have the following C code:
uint8_t firstValue = 111;
uint8_t secondValue = 145;
uint16_t temp = firstValue + secondValue;
if (temp > 0xFF) {
return true;
}
return false;
This is the alternative implementation:
uint8_t firstValue = 111;
uint8_t secondValue = 145;
if (firstValue + secondValue > 0xFF) {
return true;
}
return false;
The first example is obvious, the uint16_t
type is big enough to contain the result.
When I tried the second example with the clang
compiler on OS/X, it correctly returned true. What happens there? Is there some sort of temporary, bigger type to contain the result?
The operands of +
are promoted to larger types, we can see this by going to draft C99 standard section 6.5.6
Additive operators which says:
If both operands have arithmetic type, the usual arithmetic conversions are performed on them.
and if we go to 6.3.1.8
Usual arithmetic conversions it says:
Otherwise, the integer promotions are performed on both operands.
and then we go to 6.3.1.1
Boolean, characters, and integers which says (emphasis mine):
If an int can represent all values of the original type, the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions.48) All other types are unchanged by the integer promotions.
So both operands of +
in this case will be promoted to type int for the operation, so there is no overflow.
Note, Why must a short be converted to an int before arithmetic operations in C and C++? explains the rationale for promotions.