B
Ben Bacarisse
arnuld said:Um, not quite. You can certainly read and then modify the value of an
object with an expression; for example, "i = i + 1;" is ok.
That I know, because there is stupidity revolving here like i = i++ + 1.
Huh? Do you mean that "i = i + 1;" and "a = i;" are two different
expressions? Yes, they are (actually two different expression
statements). What's your point?
point is: If we put both of them in one expression, then we will stilll
have well-defined behavior, so why you used 2 expressions:
printf("values are: %d and %d\n", i = i + 1, a = i);
its not UB.
Yes it is. If you find the legalese hard, I find the following
useful: if you can find two (or more) valid orders of execution that
give different results then the code is UB. I am not sure that this
always works but it seems, in practise, to be pretty useful.
In your example, the result depends on the unspecified order of
evaluation of function arguments so by my rule of thumb it is UB. The
real reason is because it violates the quoted clause in the standard,
but this rule helps to see why.