Due to operator precedence (comma operator having least precedence), your code actually looks like
int a,b;
(a = 1 ? (1,2) : 3),4; // a = 2
(b = 0 ? (1,2) : 3),4; // b = 3
So, as per the ternary condition rule, quoting C11
, chapter §6.5.15
The first operand is evaluated; there is a sequence point between its evaluation and the
evaluation of the second or third operand (whichever is evaluated). The second operand
is evaluated only if the first compares unequal to 0; the third operand is evaluated only if
the first compares equal to 0; the result is the value of the second or third operand
(whichever is evaluated), converted to the type described below. 110)
[...]
110) A conditional expression does not yield an lvalue.
- For first case, the second operand is evaluated and returned.
- For second case, the third operand is evaluated and returned.