-2
public class Test
{
    public static void main(String[] args) {
        int i = 10;
        i = i++;
        System.out.println("value of i is : " + i);
    }
}

Output is : 10

When I executed similar code in C, output is 11.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • 2
    Well, the difference between you expectation and what happens is clearly because your expectation of what `i = i++` does is wrong. What more is there to say? To really answer this question we need to know *why* you think the output should be 10? – Raedwald Aug 23 '13 at 12:04
  • The dup cited does not cover Java at all, except by passing reference and definitely does not explain the difference between Java and C with respect to this example and their approaches to undefined behavior and sequence points. – Shafik Yaghmour Aug 25 '13 at 00:13

4 Answers4

14

With respect to C this is undefined behavior since you are trying to modify the same variable more than once within the same sequence point on this line:

i = i++;

the draft C99 standard in section 6.5 paragraph 2 says:

Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.

This is well defined in Java, which does not have the same sequence point concept that C does and the Java Language Specification(JLS) goes out of its way to ensure such operations are defined. Section 15.7 of the JLS says:

The left-hand operand of a binary operator appears to be fully evaluated before any part of the right-hand operand is evaluated. For example, if the left-hand operand contains an assignment to a variable and the right-hand operand contains a reference to that same variable, then the value produced by the reference will reflect the fact that the assignment occurred first. [...]

and section 15.7.2 says:

The Java programming language also guarantees that every operand of an operator (except the conditional operators &&, ||, and ? :) appears to be fully evaluated before any part of the operation itself is performed.

Note that C does not specify the order of evaluation, mainly to give the compiler better options for optimization. From the draft standard section 6.5 paragraph 3:

The grouping of operators and operands is indicated by the syntax.74) Except as specified later (for the function-call (), &&, ||, ?:, and comma operators), the order of evaluation of subexpressions and the order in which side effects take place are both unspecified.

Update

If you want a discussion on some of the differences in philosophy between Java and C with respect undefined behavior you have Undefined behavior is a design decision and Undefined behaviour in Java.

Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • Whats about c++? AFAIK the order in which the operators execute is well defined there. – Sebastian Hoffmann Aug 23 '13 at 12:06
  • 1
    @Paranaix This behavior is undefined in C++ as well section `1.9` paragraph 15 in the draft standard. – Shafik Yaghmour Aug 23 '13 at 12:14
  • But cant it be written as `i = i.operator++()`? I know well that `i` isnt a object, but I try to explain my point of view with that pseudo code. – Sebastian Hoffmann Aug 23 '13 at 12:22
  • @Paranaix I apologize, I did not respond earlier but you are correct, in that instance right before the function is called introduces a sequence point, although the language has changed in the newer draft standards. – Shafik Yaghmour Aug 24 '13 at 02:35
5

This is Undefined behaviour in C. Lack of sequence point.

Dayal rai
  • 6,548
  • 22
  • 29
  • 2
    This is also defined behavior in Java, where the variable's value is saved, the variable is incremented, and the old value written back. http://stackoverflow.com/questions/7911776/what-is-x-after-x-x – chrylis -cautiouslyoptimistic- Aug 23 '13 at 12:02
0

In Java, i = i++; post ++ will increment value after that step. But here at the moment of assigning i++ to i, i still is 10 so you are getting 10. But in C this may different from Java. If you use i=++i in Java code, you will get 11.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

Java and C are different languages with different rules. In C, the exact order in which expressions are evaluated and side effects are applied is unspecified; for an expression like i = i++, there's no guarantee that the side effect of the ++ operator will be applied before the assignment takes place. The result will vary depending on the platform, optimization settings, even the surrounding code. The behavior is undefined; the compiler can handle the situation any way it sees fit, which includes generating an unexpected result.

Java, OTOH, does specify a strict order of evaluation and that side effects are applied immediately, so the expression is well-defined.

John Bode
  • 119,563
  • 19
  • 122
  • 198