ProgrammingHardHCL · Tech Mahindra pattern

What is printed?

What is printed?
void f(void) {
    int x = 5;
    x = x++ + ++x;
    printf("%d", x);
}
int main(void) { f(); }
  1. A. Undefined behaviour: x is modified more than once between sequence points without a defining intervening sequencing
  2. B. 11
  3. C. 13
  4. D. 12

Correct answer: A. Undefined behaviour: x is modified more than once between sequence points without a defining intervening sequencing

x++ and ++x both modify x within the same expression `x++ + ++x`, with no sequence point separating these modifications (and the outer assignment to x adds yet another unsequenced write to the same object) — this is classic undefined behaviour in C (pre-C++17 rules for C still apply; C has no sequencing guarantee here), not a single well-defined numeric result.

More C Programming questions

Exams that ask this topic