1

This statement int b = (a) + a++; is supposed to assign the value 8 to variable b, where a is initially assigned the value 4. However, the output of the program is 9 instead of the expected 8.

I expect 8 to be output because () and ++ (post) have same precedence and so by their associativity rule (a) should be evaluated first and then a++, both of which evaluate to 4. Hence we should have 4 + 4, which equals '8'.

I would like to understand the reasons behind this unexpected result. Why is the output 9 instead of 8? How does the order of evaluation and the behavior of the increment operator contribute to this outcome?

Osmium
  • 169
  • 5
  • 3
    Precedence does not dictate order of evaluation (see e.g. here: https://stackoverflow.com/questions/67689209/operator-precedence-versus-order-of-evaluation) – Mat Jun 25 '23 at 07:14
  • Save/Print [Associativity&Precedence Order Chart](https://www.cs.uic.edu/~i109/Notes/COperatorPrecedenceTable.pdf) and learn from the top down approach, it will make sense – t0mm13b Jun 25 '23 at 07:54
  • This question was closed to an inappropriate duplicate. This is not a duplicate of [**Why are these constructs using pre and post-increment undefined behavior?**](https://stackoverflow.com/questions/949433/why-are-these-constructs-using-pre-and-post-increment-undefined-behavior). That question involves multiple unsequenced assignments to a single variable. There is only one assignment to `a` in this question. I've reopened the question so that it either gets a proper answer or, worst case, gets closed to an actual duplicate. – Andrew Henle Jun 25 '23 at 13:31
  • `int b = (a) + a++;` is absolutely equivalent to `int b = a + a++;` or `int b = (a) + ((((a++))));` or `int b = ((((a))) + ((((((a++))))))));` or ... and, as said in other comments, **Undefined Behaviour**. – pmg Jun 25 '23 at 13:48
  • 1
    @AndrewHenle It looks like that question includes cases like `printf("%d %d\n", ++w, w);` that are applicable here, and would be appropriate. – dbush Jun 25 '23 at 14:09
  • @pmg I'm pretty certain `int b = (a) + a++;` is not undefined behavior, but it is unspecified behavior. Undefined behavior would be invoked with multiple unsequenced assignments, but that's not present. – Andrew Henle Jun 25 '23 at 14:25
  • @dbush Yes, a full reading of the question and its answers does address that, but the title of the question is **Why are these constructs using pre and post-increment undefined behavior?**, IMO making the dupe reason wrong. For example, even [this answer](https://stackoverflow.com/a/51876456/4756299) seems to characterize the behavior of code very similar to `printf("%d %d\n", ++w, w);` as "undefined", when I'm pretty sure it's "unspecified". That's a subtle but IMO important difference. – Andrew Henle Jun 25 '23 at 14:33
  • @AndrewHenle UB doesn't necessarily have to be two unsequenced writes, it can be an unsequenced read and write as in this example. C11s6.5p2: *"If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object **or a value computation using the value of the same scalar object**, the behavior is undefined.*" – dbush Jun 25 '23 at 15:39

0 Answers0