Behaviour of PreIncrement and PostIncrement operator in C and Java [duplicate]
Asked Answered
M

2

6

I'm running the following programs in Visual C++ and Java:

Visual C++

void main()
{
    int i = 1, j;
    j = i++ + i++ + ++i;
    printf("%d\n",j);
}

Output:

6

Java:

public class Increment {
    public static void main(String[] args) {
        int i = 1, j;
        j = i++ + i++ + ++i;
        System.out.println(j);
    }
}

Output:

7

Why the output in these two languages are different? How both the langauges treat pre and postincrement operators differently?

Middlebreaker answered 7/7, 2013 at 18:6 Comment(1)
In C and C++, the order in which expressions are evaluated and the order in which side effects are applied are unspecified; the result will vary from implementation to implementation. The respective standards leave such behavior undefined so that the compiler implementor doesn't have to worry about how to handle such expressions; any result is considered "correct". Java and C#, OTOH, specify that all expressions are evaluated from left-to-right, and that all side effects are applied immediately, so expressions like this are well-defined.Wooded
T
2

In C/C++ behavior is undefined because In this expression i is modified more then once without an intervening sequence point. read: What's the value of i++ + i++?

Of-course in Java behaviour of this kind of codes is well defined. Below is my answer for Java, step by step:

At the beginning i is 1.

j = i++ + i++ + ++i;
// first step, post increment
j = i++ + i++ + ++i;
//  ^^^
j = 1   + i++ + ++i;
// now, i is 2, and another post increment:
j = i++ + i++ + ++i;
//  ^^^^^^^^^
j = 1   + 2   + ++i;
// now, i is 3 and we have a pre increment:
j = i++ + i++ + ++i;
//  ^^^^^^^^^^^^^^^^
j = 1   + 2   +   4;
j = 7;
Trioecious answered 7/7, 2013 at 18:18 Comment(1)
your answer is very good for Java, I added for c in your answer, if you don't like roll back to your version.Herrera
M
4

The C++ example evokes undefined behavior. You must not modify a value more than once in an expression. between sequence points. [Edited to be more precise.]

I'm not certain if the same is true for Java. But it's certainly true of C++.

Here's a good reference:
Undefined behavior and sequence points

Militant answered 7/7, 2013 at 18:10 Comment(2)
citation please? I've never heard of that... only that it was not recommended.Crackling
@Mgetz: Edited above to add a definitive reference. Scroll down to "1) Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression."Militant
T
2

In C/C++ behavior is undefined because In this expression i is modified more then once without an intervening sequence point. read: What's the value of i++ + i++?

Of-course in Java behaviour of this kind of codes is well defined. Below is my answer for Java, step by step:

At the beginning i is 1.

j = i++ + i++ + ++i;
// first step, post increment
j = i++ + i++ + ++i;
//  ^^^
j = 1   + i++ + ++i;
// now, i is 2, and another post increment:
j = i++ + i++ + ++i;
//  ^^^^^^^^^
j = 1   + 2   + ++i;
// now, i is 3 and we have a pre increment:
j = i++ + i++ + ++i;
//  ^^^^^^^^^^^^^^^^
j = 1   + 2   +   4;
j = 7;
Trioecious answered 7/7, 2013 at 18:18 Comment(1)
your answer is very good for Java, I added for c in your answer, if you don't like roll back to your version.Herrera

© 2022 - 2024 — McMap. All rights reserved.