Search code examples
cundefined-behaviorinteger-overflow

Integer overflow in intermediate arithmetic expression


This might be a very basic programming question, but it's something I've wanted to understand for some time.

Consider this simple example:

int main(void) 
{
  unsigned char a = 5;
  unsigned char b = 20;
  unsigned char m = 0xFF;

  unsigned char s1 = m + a - b;
  unsigned char s2 = m - b + a;
  printf("s1 %d s2 %d", s1, s2);
  return 0;
}

Given that arithmetic operators are evaluated from left to right in C, the first calculation here should overflow at m + a. However, running this program returns the same answer for s1 and s2. My question here is: does the first expression lead to undefined behavior because of the overflow? The second expression should avoid the overflow, but I wanted to understand why the two expressions return the same answer.


Solution

  • According to the ISO C specification §6.2.5.9

    A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type.

    This means that both the would-be positive and negative overflows that seem to occur in your addition and subtraction respectively are actually performed as signed int so they are both well-defined. After the expression is evaluated, the result is then truncated back to an unsigned char since that's the left-hand result type.