ProgrammingHardIBM · Deloitte pattern

What is printed?

What is printed?
int f(void) { printf("f "); return 1; }
int g(void) { printf("g "); return 1; }
int main(void) {
    if (f() || g())
        printf("yes ");
}
  1. A. f yes
  2. B. g yes
  3. C. f g yes
  4. D. g f yes

Correct answer: A. f yes

|| guarantees left-to-right evaluation with short-circuiting: f() is called first, printing "f ", and returns 1 (true), so g() is never evaluated at all (the right side of || is skipped once the left side is truthy); the condition is true, so "yes " prints.

More C Programming questions

Exams that ask this topic