ProgrammingVery hardTCS NQT · Infosys

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    unsigned char a = 100, b = 200;
    unsigned char s = (unsigned char)(a + b) / 2;
    printf("%d", s);
}
  1. A. 150
  2. B. 172
  3. C. 100
  4. D. 22

Correct answer: D. 22

a + b is 300 in int, but the cast to unsigned char wraps it to 44 before the division, giving 22. Dividing first would give 150.

More C Programming questions

Exams that ask this topic