Consider:
int m = 2, n;
n = m++ + (++m);
In C output is:
m = 4, n = 4;
In Java output is:
m = 4, n = 5;
How does this happen?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It can differ because C does not allow a correct program to contain such an expression – C does not define the behaviour of such a program. This gives C compilers wide latitude in how they interpret such expressions.
Java more tightly constrains implementations by defining the expected behaviour of expressions like this.
(The rule that this breaks in C is that an expression may not modify the value of an object more than once without an intervening sequence point).