ProgrammingHardTCS NQT · Infosys pattern

Assume 32-bit int. What is printed?

Assume 32-bit int. What is printed?
int x = 7;
int y = x > 5 && x < 10 ? x * 2 : x + 1;
printf("%d", y);
  1. A. 8
  2. B. 14
  3. C. Compile-time error: condition needs extra parentheses
  4. D. 7

Correct answer: B. 14

&& binds tighter than ?:, so the condition is (x > 5 && x < 10), i.e. (7>5) && (7<10) = true && true = true. The ternary then picks the true-branch, x * 2 = 14.

More C Programming questions

Exams that ask this topic