[Solved] What happens when a integer overflow occurs in a C expression?
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?
Read more here: Source link