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

Discussion in 'MCSD' started by Shailendra Sharma, Jun 29, 2003.

  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? :p
    >
    >Regards,
    >
    >Caio Proiete
    >MCSD (Visual C++)
    >.
    >
    Shailendra Sharma, Jun 29, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    13
    Views:
    1,090
    Martin Bodenstedt
    Oct 31, 2005
  2. clyclopedic
    Replies:
    1
    Views:
    1,189
    Shailendra Sharma
    Jun 29, 2003
  3. Replies:
    6
    Views:
    908
    petrolhead
    May 12, 2005
  4. Tuna Fish
    Replies:
    0
    Views:
    390
    Tuna Fish
    Nov 20, 2003
  5. RichA
    Replies:
    3
    Views:
    293
    PeterN
    Nov 29, 2011
Loading...

Share This Page