Please determine the output
-
Sushant Duggal wrote:
cout< This code is making some assumptions that are now being shown for it to compile as C++. It's not going to compile for Java. Beyond that, I agree with the other reply, you need to do your own homework.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
Thanks for your reply friends, But I just want to know the reason why the outputs differ. i In C++, The result is x=18.y=7 where as in Java, The result is x=19, and y=7 I want to know the reason for this output. Is there a difference of stack implementation in the two languages or there is some other reason.. Please let me know. Thanks,
Sushant Duggal.
-
Thanks for your reply friends, But I just want to know the reason why the outputs differ. i In C++, The result is x=18.y=7 where as in Java, The result is x=19, and y=7 I want to know the reason for this output. Is there a difference of stack implementation in the two languages or there is some other reason.. Please let me know. Thanks,
Sushant Duggal.
++x guarentees that the number is incremented before it's returned. x++ guarentees it's incremented after, but not how LONG after.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
++x guarentees that the number is incremented before it's returned. x++ guarentees it's incremented after, but not how LONG after.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
Yes, I agree on your comment. so do you want to say that there is no method to determine the value?
Sushant Duggal.
-
++x guarentees that the number is incremented before it's returned. x++ guarentees it's incremented after, but not how LONG after.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
OK now i got the point. its correct to get 19 in C++, but I still didnt understand why it comes 18 in java. Can you comment on this? Thanks
Sushant Duggal.
-
OK now i got the point. its correct to get 19 in C++, but I still didnt understand why it comes 18 in java. Can you comment on this? Thanks
Sushant Duggal.
I believe it goes like this: The ++ operator occurs in both languages AFTER x is given it's final value. The difference is that in Java, the values are by reference, so the value that x is assigned to, is the value that still has a ++ operator outstanding. so, x gets set to 18. y gets incremented, to 7, and x also gets incremented, but it's the same x that is 18, not a copy of x which is 16.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
Hi friends, Can anyone tell me what will be the output of the folloing code and whats the reason for that. It will be a great help.
int x = 12; int y = 6; x = x++ + y++ cout< can you tell me the output for C++ and Java compiler and whats the reason for the answer. Thanks in Advance Sushant Duggal.
Oh - you also posted in the wrong forum, I didn't notice before.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
Oh - you also posted in the wrong forum, I didn't notice before.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
Then where I would have posted this?
Sushant Duggal.
-
Then where I would have posted this?
Sushant Duggal.
Managed C++/CLI is the forum for discussion of .NET C++, as it clearly states. This[^] is the general C++ forum.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
Managed C++/CLI is the forum for discussion of .NET C++, as it clearly states. This[^] is the general C++ forum.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
The ++ operator occurs in both languages AFTER x is given it's final
value. The difference is that in Java, the values are by reference, so
the value that x is assigned to, is the value that still has a ++ operator outstanding. so, x gets set to 18. y gets incremented, to 7,
and x also gets incremented, but it's the same x that is 18, not a
copy of x which is 16.I can understand that in java, the values are by reference. that means X is incremented direectly where it is stored, it sets it to 12 + 6 = 18. Confusion ..... when the X is going to incremented? and why it is not reflected(X's value is still 18). Thanks For your Patience, Sushant Duggal. -- modified at 3:39 Thursday 28th September, 2006
Sushant Duggal.
-
Hi friends, Can anyone tell me what will be the output of the folloing code and whats the reason for that. It will be a great help.
int x = 12; int y = 6; x = x++ + y++ cout< can you tell me the output for C++ and Java compiler and whats the reason for the answer. Thanks in Advance Sushant Duggal.
The statement i = i++ has undefined behavior in C++ and different compilers can generate different code and still be standards compliant. The = operator does not define a separate sequence point, and C/C++ rules dictate that an expression can modify an object's value only once within a single sequence point - else the compiler's free to interpret the code in whatever way it wants to.
Regards, Nish
Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
Currently working on C++/CLI in Action for Manning Publications. Also visit the Ultimate Toolbox blog