0

my code is :

 code(){
    int x=7;
    x=x++;
    output x;   //prints 8 in C, prints 7 in Java
 }

Guys the above code: prints 8 in C, and 7 in Java !!

Why is this so? please explain.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
gursahib.singh.sahni
  • 1,559
  • 3
  • 26
  • 51

6 Answers6

8

That will print 7 in Java. x=x++; is equivalent to :

int temp = x;
x = x + 1;  
x = temp;

The result would have been different if you would have used prefix operator , ++x .

See for yourself over here: java code; C code.

Read Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…) to comprehend the output in C.

Community
  • 1
  • 1
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • @MarkoTopolnik Thanks for that Ideone proof. – AllTooSir Aug 01 '13 at 11:45
  • Sir i have run this code.. i have seen this explanation but why this behaviour different in C ? :/ – gursahib.singh.sahni Aug 01 '13 at 11:45
  • @anonymous It is not different. – Marko Topolnik Aug 01 '13 at 11:50
  • @anonymous And also read this :http://stackoverflow.com/questions/949433/could-anyone-explain-these-undefined-behaviors-i-i-i-i-i-etc – Suresh Atta Aug 01 '13 at 11:51
  • @anonymous. The behaviour is undefined in `C`. It can give you different output based on different compilers, and environment. – Rohit Jain Aug 01 '13 at 11:53
  • @anonymous: Java and C are different languages with different rules, and this is one of those situations where the rules are *very* different. Java guarantees that all expressions are evaluated left-to-right, and that the side effects of the ++ operators are applied immediately. C does not make this guarantee, so the result will vary from platform to platform. – John Bode Aug 01 '13 at 19:21
6

In Java, x=x++, is evaluated as:

int temp = x;
x = x + 1;
x = temp;

So, basically there is no change is x after that expression.

In C however, that expression is an Undefined Behaviour. Also see Sequence Points Wiki

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • probably his mean to say that his C code printing `8` that is because in his compiler: `x = x++;` beaked in two parts: `x = x;` then `x = x + 1;` because of `++`. But yes its compiler dependent in C. – Grijesh Chauhan Aug 01 '13 at 11:51
  • @GrijeshChauhan. Well, yes. That is what I've written in my answer. I've not talked about output in `C`, as it's undefined there. In Java, it's guaranteed to be `7`, while OP said it's `8`. – Rohit Jain Aug 01 '13 at 11:52
  • check again OP says in Java `7` but in C he is getting `8`. – Grijesh Chauhan Aug 01 '13 at 11:55
  • @GrijeshChauhan. Ah! I think he changed it. – Rohit Jain Aug 01 '13 at 11:56
5

This code cause undefined behaviour in C so the result may be any, 7, 8, 15 or Page fault. Why this code give 7, is compiler matter.

Vovanium
  • 3,798
  • 17
  • 23
1

In the Java background, something like the following occurs (for the i = i++ statement):

int temp = i; // store current value of i  
i = i + 1; // increase i because of i++  
i = temp; // assign to i  
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
1
x=x++;

This gives arbitrary results in C, mainly depending on compiler. Read about sequential points in C. You may refer to C Programming by Dennis ritchie.

user2550754
  • 884
  • 8
  • 15
0

it is because of operator precedence. = has more precedence in C than Java.

Mert Gülsoy
  • 2,779
  • 1
  • 19
  • 23
  • Wrong. This prints 7 in Java. And the precedences are all the same. – Marko Topolnik Aug 01 '13 at 11:45
  • In C, it is compiler-specific, i.e. undefined, while in Java it is defined in the language spec (rhs evaludated completely, including side-effects, before assignment to lhs takes place). – TheBlastOne Aug 01 '13 at 11:52