3 ms·
Hmm? Which part implies “all bets are off”? There’s the condition (evaluated first), the true case and the false case. Only one of the cases is evaluated. I’m
by boulos 6y ago
Hmm? Which part implies “all bets are off”?
There’s the condition (evaluated first), the true case and the false case. Only one of the cases is evaluated.
I’m assuming you were alluding to some sort of “x++ = x++ * *x++” style situation, but the ternary expression isn’t a statement and there are sequence points in between. As a bonus, the first result for “ternary operator evaluation order” gave me someone who linked to the standard [1]. Quoting from the standard that they quoted:
> 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.
[1] https://stackoverflow.com/questions/65109494/order-of-evaluation-for-ternary-operator-in-c https://stackoverflow.com/questions/65109494/order-of-evalua...
- moonbug 6y ago> Hmm? Which part implies “all bets are off” Remember, the received wisdom of HN programmers is the C is black magic.
- microtherion 6y agoI was not thinking about local state; you're right that the sequence points ensure an orderly evaluation there. My point was that since ternaries can call functions, and functions can have side effects on global state, the concept of an "if" being a "statement" vs a ternary being an "expression / value" is not nearly as meaningful as it's made out to be. A ternary can trade stocks, fire a 500W laser, unlock a velociraptor enclosure, etc, just as easily as an if can.