Given uint8_t a = 200, b = 100; what is the type and value of the expression a + b in a typical C implementation?
Correct answer
D. int with value 300, because both operands are promoted to int before the addition
13 views
D. int with value 300, because both operands are promoted to int before the addition
13 views
Integer promotion converts operands narrower than int to int before arithmetic, so the uint8_t values 200 and 100 are added as ints and give 300 rather than 44. The wrap to 44 appears only when the result is assigned back to a uint8_t object, a narrowing conversion defined modulo 256. On a target whose int is 16 bits, unsigned int becomes the promoted type instead.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is D? Ask here and get an explanation.