ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
int a = 1, b = 0, c = 1;
int result = a || b && c;
printf("%d", result);
  1. A. 1, and && is evaluated first because it binds tighter than ||
  2. B. 0
  3. C. 1, but only because of left-to-right evaluation with no precedence involved
  4. D. Compile-time error: ambiguous expression, needs explicit parentheses

Correct answer: A. 1, and && is evaluated first because it binds tighter than ||

&& has higher precedence than ||, so a || b && c parses as a || (b && c). Since a is 1 (true), the || short-circuits immediately: the whole expression is true (1) without even needing to evaluate b && c at all, though if evaluated it would be (0 && 1) = 0 — either way the overall result is 1.

More C Programming questions

Exams that ask this topic