![]() |
|
|
|||||||
![]() |
MCSD - One code, 2 different results C# vs C++ |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
The difference lies in handling of extended assignment
operators such as +=, -=, /= etc etc in different languages such as Java or C# versus C++. In Java it is handled like this... <variable><op>=<expression> the symantics for this are <variable> = (<type>)(<variable><op>(<expression>)) Note, in Java the <variable> is evaluated just once upon entry v/s C++ where the latest values for the variable is used, hence the difference. Please refer to "Programmers Guide to Java Certification by Khalid Mughal, page 53" for reading up on Java's handling of Extended Assignment operators. I do not think it is operator precedence. Extended assignment operators have same precedence as assignment operators which is the lowest in the precedence table, --, ++ are ranked much much higher than assigment operators.... >-----Original Message----- >Hi, I was just studying for my 70-315 test and had one >surprise. Look at this code: > >int myFunc() >{ > int x; > x = 42; > x++; > x += --x; > return x; >} > >What is the value returned from myFunc()? >As a C++ programmer I said: "84 of course". > >But when I compiled with C#, the function returns "85". >Just to confirm, I also compiled the same code with Java >and returned 85 either. > >Well... can anyone explain this? > >Regards, > >Caio Proiete >MCSD (Visual C++) >. > Shailendra Sharma |
|
|