1. Home
  2. Computer Science MCQs
  3. C Programming
  4. Given uint8_t a = 200, b = 100; what is the type and value…

Added 30 Nov -0001

Given uint8_t a = 200, b = 100; what is the type and value of the expression a + b in a typical C implementation?

  1. uint8_t with value 44, because the sum wraps at 256 and is truncated back down
  2. unsigned int with value 300, because uint8_t promotes to unsigned int on all targets
  3. int with value 44, because the sum is computed in 8 bits and then sign-extended up
  4. int with value 300, because both operands are promoted to int before the addition Correct answer

Correct answer

D. int with value 300, because both operands are promoted to int before the addition

13 views

MCQSEXAM

Explanation

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.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is D? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C Programming MCQs

Practise all C Programming MCQs

Similar MCQs