By solving this, i got 5592, 55 for first x by adding both x and y before increment them ,since they are post increment operators, then in next expression both x and y get incremented and added to give y value as 92. Right? But the computer shows the answer as 5794. How is it possible?
Expressions has direct effects and side effects.
This expression, x = y; has a direct effect of copying the computed value of y assigning it to the location x.
x = a[i]; has a direct effect as well. x = a[i++]; has a direct effect and a side effect. After the value of a[i] gets copied to x, one is added to the value of i. This works fine because of the sequence points. After the statement has completed execution, both x and i has its value changed in a predictable way.
x = a[i++] + b[i++]; in the other hand is unpredictable. The precedence of the operators are defined by the C Standards but the order of execution is not. It is possible that a[i] + b[i] gets added first and then assign to x, or it is possible that i++ gets increment first and then the sum of a[i] + b[i] gets assign to x.
Heck, it is even possible (anything is possible) that in a bizarre way i gets incremented for the a subscript and then for the b subscript before the sum of both gets assign to x
The rule of thumb is not to rely on the order of precedence between sequence points. A sequence point is the guarantee that all direct and side effects are completed at that point. Sequence points are:
?: ( question mark and colon)
and any expression as a parameter for a function call.
Now, if you look back to the previously posted x=y++ + x++; we can see that it is not possible to know in a predictable way the final value of x, taking in consideration what I just explained. However, there's more. There's a rule in the standard that says that between sequence points a object can have its value changed only once, and that the previous value shall be access only for determining the value stored.
This statement x = x++; would break that rule.