ProgrammingHardGATE CS

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    int a = 0, b = 1, c = 2;
    int r = (a++ && b++) || (c-- && a--);
    printf("%d %d %d %d", r, a, b, c);
    return 0;
}
  1. A. 1 0 2 1
  2. B. 1 0 1 1
  3. C. 1 1 1 1
  4. D. 0 0 2 1

Correct answer: B. 1 0 1 1

a++ yields 0 (a becomes 1), so && skips b++ and the left side is 0. The right side of || is then evaluated: c-- yields 2 (c becomes 1), which is true, so a-- runs (yields 1, a becomes 0). r = 1, a = 0, b = 1, c = 1.

More C Programming questions

Exams that ask this topic