Home > Archive > microsoft.public.cert.exam.mcsd > June 2003 > One code, 2 different results C# vs C++





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author One code, 2 different results C# vs C++
Shailendra Sharma

2003-06-29, 9:23 am

Xref: intern1.nntp.aus1.giganews.com microsoft.public.cert.exam.mcsd:21965

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? :P
>
>Regards,
>
>Caio Proiete
>MCSD (Visual C++)
>.
>

Sponsored Links





Free Braindumps | MCSE braindumps software forum

Copyright 2003 - 2008 examnotes.net