Go Back   Velocity Reviews > Newsgroups > MCSD
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

MCSD - One code, 2 different results C# vs C++

 
Thread Tools Search this Thread
Old 06-29-2003, 02:51 PM   #1
Default One code, 2 different results C# vs C++


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
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump