ProgrammingVery hardProduct company OA

What is printed?

What is printed?
int n = 0;
int t(void) { n++; return 1; }
int f(void) { n++; return 0; }
int main(void) {
    int r = f() || t() && f() || t();
    printf("%d %d", r, n);
}
  1. A. 1 4
  2. B. 1 3
  3. C. 1 2
  4. D. 0 3

Correct answer: A. 1 4

&& binds tighter, so this is f() || (t() && f()) || t(). f() gives 0, so the && runs: t() is 1 and f() is 0, giving 0. The last t() then gives 1. All four calls run, and r = 1. '1 2' wrongly short-circuits after t().

More C Programming questions

Exams that ask this topic