The first one:
you have one ++x so x becomes 11 (preceeding increment is done before the variable is taken in the other operation)
after that is executed the summing x + x, so the sum becomes 22
and finally is executed x++ so x becomes 12.
The second one:
the first two x are summed up (they are with postfix inc so they are still 10)
at this point the sum is 20
after that this sum has to be summed up with the thirs x, but there we have prefix inc so x becomes 11, and the sum becomes 20+11=31
after that the postfix increment of the first two x is done so x becomes 13.
The third one:
The first two x are summed up, the one of them is wirh prefix inc, so x becomes 11 and the sum at this point is 22. After that the thirs x comes, it is with postfix inc so the x is still 11 and the sum becomes 33. After that the 4th x comes, it is with prefix inc so x becomes 12 and the sum becomes 45. after that the two postfix increments are done and the x becomes 14.
In fact java has same precedence and associativity as C. I get a different result under java. I also, feel the the resut in Java adheres to the precedence.
0
Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.
Yes, the increment operator has higher precedence but the associativity of + is left to right.
Yo can look at sum = x++ + ++x + x++ + ++x as sum = (((x++ + ++x) + x++) + ++x)
In fact these things are not defined well in any C standard, so although probably the most compilers will produce similar code, it is not good to rely on them in practice. They have to be used in such way just for some puzzling.
int sum = x++ + ++x;
violates the requirement that
Between the previous and next sequence point an object shal have its stored value modified
at most once by the evaluation of an expression.
which means the behaviour is undefined.
0
Featured Post
Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.
The first one:
you have one ++x so x becomes 11 (preceeding increment is done before the variable is taken in the other operation)
after that is executed the summing x + x, so the sum becomes 22
and finally is executed x++ so x becomes 12.
The second one:
the first two x are summed up (they are with postfix inc so they are still 10)
at this point the sum is 20
after that this sum has to be summed up with the thirs x, but there we have prefix inc so x becomes 11, and the sum becomes 20+11=31
after that the postfix increment of the first two x is done so x becomes 13.
The third one:
The first two x are summed up, the one of them is wirh prefix inc, so x becomes 11 and the sum at this point is 22. After that the thirs x comes, it is with postfix inc so the x is still 11 and the sum becomes 33. After that the 4th x comes, it is with prefix inc so x becomes 12 and the sum becomes 45. after that the two postfix increments are done and the x becomes 14.
Hopes that helps!