What will be the output of the following C program? #include <stdio.h> int…
2025
What will be the output of the following C program?
#include <stdio.h>
int main()
{
int x = 10;
printf("%d %d", x++, ++x);
return 0;
}
- A.
10 12
- B.
11 12
- C.
10 11
- D.
12 12
- E.
Undefined behavior
Attempted by 24 students.
Show answer & explanation
Correct answer: E
Concept: C requires a sequence point between two modifications of the same object: between one sequence point and the next, a scalar object may be modified by an expression's evaluation at most once, and may only be read for the purpose of computing that stored value. Breaking this rule makes the expression's behaviour undefined — the compiler is free to produce any output, not merely the value a human might expect from left-to-right reasoning.
Application: In printf("%d %d", x++, ++x);, x is modified twice — once by x++ and once by ++x — as two arguments of the same function call. C does not fix an order in which function arguments are evaluated, and there is no sequence point separating the evaluation of one argument from another, so the two modifications of x are unsequenced with respect to each other. This exactly matches the forbidden pattern: the same scalar object is written more than once between sequence points, with no way to know which write happens first. Different compilers (and even different optimisation levels of the same compiler) can legally print different pairs of numbers — none of the fixed left-to-right values is guaranteed.
Cross-check: This is confirmed by the C standard’s own definition of sequence points (WG14 draft, section 6.5) and by the well-known EXP30-C secure-coding rule, "do not depend on the order of evaluation for side effects." Because no evaluation order can be assumed, no single numeric pair is the correct output — the only description of the program’s behaviour that the standard actually guarantees is "undefined behavior."