• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

confusion .....

 
udit agarwal
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey friends....

i m going to write the Scjp 1.4 on 10th june...
i have a confusion regarding to the code below..

int i;
i=i++;

wat will be the value of i

First when i saw it..i think that the ootput will be 1 but.i was shocked when the output show 0....

then i run the same code on c and c++ compiler then it shows 1...

so can any one tell me why it show 0.....

Udit Agarwal
 
Dharmesh Gangani
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The key here is to understand how the post-increment operator works as well as the order in which expressions are evaluated.

The post-increment operator works in this way -
1. return the current value of i (= 0)
2. increment the current value and store it in variable i. (i = 1)

Now, the value returned by the post-increment operation (i.e. 0) is assigned to the LHS variable i, overwriting the previously written value 1.

Thus, i = i++; leaves i = 0;

Hope, this has cleared ur doubt.
 
udit agarwal
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey..

Nice to see the answer......
but why it shows 1 when complile in c and c++

Is the post increment operator works differently in c anc c++
 
Dharmesh Gangani
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I m not exactly aware about C & C++.
 
Naseem Khan
Ranch Hand
Posts: 809
 
Jacob Thomas
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I posted the same doubt couple of weeks back. I never got any answer. The i=i++ statement works differently in C and C++. It gives the answer 1. But my reasoning is that post-increment operator is one of the fundamental operators and its behaviour should be the same irrespective of implementation language. I understand the working of i++ statement in Java, but I am still not convinced!!! And I believe that's why most of the people find it so hard to understand the output in Java as majority of us come to Java after a stint in C and C++.
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah dats true, one thing u must remember, c,c++ and java are totally different even at operator level.
So never mix them
 
neetika sharma
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i=i++;

I got some explantion like this, quite convinced I was with this so sharing it....


What happens with that code is this:

Let's say 'i' has a starting value of 0. The expression 'i++' evaluates to the pre-increment (original) value of 'i', or 0. The result of this expression (0) is remembered. The value of 'i' is then incremented ('i' is now 1). The '=' assignment operator has the least precedence, so it happens last: the result of the right-hand expression (what we remembered earlier, namely 0) is assigned back to 'i'. Therefore, the result of the whole statement is that 'i' has a value of 0.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic