ProgrammingHardWipro · Cognizant pattern

What is printed?

What is printed?
int main(void) {
    int a = 2, b = 3;
    int result = a++ * b-- + --a + ++b;
    printf("%d %d %d", a, b, result);
}
  1. A. 7 3 9
  2. B. Undefined behaviour: a and b are each modified more than once in this expression via multiple increment/decrement operators without sufficient sequencing between their uses
  3. C. 8 3 9
  4. D. 8 3 10

Correct answer: B. Undefined behaviour: a and b are each modified more than once in this expression via multiple increment/decrement operators without sufficient sequencing between their uses

Both a (via a++ then --a) and b (via b-- then ++b) are modified more than once within this single expression, with no sequence points between the respective operations on each variable — this is undefined behaviour, not a single well-defined arithmetic result.

More C Programming questions

Exams that ask this topic