Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. One code, two different results - C# vs C++

One code, two different results - C# vs C++

Scheduled Pinned Locked Moved C#
csharpquestionc++javavisual-studio
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    C Augusto Proiete
    wrote on last edited by
    #1

    As I was practicing my C# skills I came across the code below: ```csharp int myFunc() { int x; x = 42; x++; x += --x; return x; } ``` What is the value returned from `myFunc()`? As a C++ programmer I thought: "`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` too. Well... Can anyone explain this? :P

    C. Augusto Proiete https://augustoproiete.net

    N 1 Reply Last reply
    0
    • C C Augusto Proiete

      As I was practicing my C# skills I came across the code below: ```csharp int myFunc() { int x; x = 42; x++; x += --x; return x; } ``` What is the value returned from `myFunc()`? As a C++ programmer I thought: "`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` too. Well... Can anyone explain this? :P

      C. Augusto Proiete https://augustoproiete.net

      N Offline
      N Offline
      Nick Seng
      wrote on last edited by
      #2

      :confused: I don't see how C++ would return 84?? int x; x = 0 x = 42; x = 42 x++; x = 43 x += --x; == x = x + (--x) == x = 43 + 42 x = 85 Is there something I'm missing?? Admittedly, I haven't done C++ in a while Nick Seng (the programmer formerly known as Notorious SMC)


      God, I pity me! - Phoncible P. Bone

      L 1 Reply Last reply
      0
      • N Nick Seng

        :confused: I don't see how C++ would return 84?? int x; x = 0 x = 42; x = 42 x++; x = 43 x += --x; == x = x + (--x) == x = 43 + 42 x = 85 Is there something I'm missing?? Admittedly, I haven't done C++ in a while Nick Seng (the programmer formerly known as Notorious SMC)


        God, I pity me! - Phoncible P. Bone

        L Offline
        L Offline
        leppie
        wrote on last edited by
        #3

        Nick Seng wrote: x += --x; == x = x + (--x) That should be: x--; (42) x = x + x; (84) leppie::AllocCPArticle(Generic DFA State Machine for .NET);

        N B 2 Replies Last reply
        0
        • L leppie

          Nick Seng wrote: x += --x; == x = x + (--x) That should be: x--; (42) x = x + x; (84) leppie::AllocCPArticle(Generic DFA State Machine for .NET);

          N Offline
          N Offline
          Nick Seng
          wrote on last edited by
          #4

          hmmm..... Shouldn't it be left to right(order of operation) ? I mean: x = x + (--x) : At the beginning of this line x is still 43 x = x + (--x) : which would imply that this x is still 43 thus, you would get 85? [Edit]Something just occured to me, does, --x imply that the incrementer execute before any other operators? [/Edit] Nick Seng (the programmer formerly known as Notorious SMC)


          God, I pity me! - Phoncible P. Bone

          J 1 Reply Last reply
          0
          • N Nick Seng

            hmmm..... Shouldn't it be left to right(order of operation) ? I mean: x = x + (--x) : At the beginning of this line x is still 43 x = x + (--x) : which would imply that this x is still 43 thus, you would get 85? [Edit]Something just occured to me, does, --x imply that the incrementer execute before any other operators? [/Edit] Nick Seng (the programmer formerly known as Notorious SMC)


            God, I pity me! - Phoncible P. Bone

            J Offline
            J Offline
            jspano
            wrote on last edited by
            #5

            The -- operator has precedence over the += so it goes first makeing x 42 then the += is 42 + 42. Maybe this is a bug in c#? -- is supposed to go first in c# also. Maybe it doens't update x until the whole statement is done, and only returns 42 to the statement so you get 43 + 42.

            C 1 Reply Last reply
            0
            • J jspano

              The -- operator has precedence over the += so it goes first makeing x 42 then the += is 42 + 42. Maybe this is a bug in c#? -- is supposed to go first in c# also. Maybe it doens't update x until the whole statement is done, and only returns 42 to the statement so you get 43 + 42.

              C Offline
              C Offline
              C Augusto Proiete
              wrote on last edited by
              #6

              Hi! I've posted this same question in the Microsoft C# Newsgroups and they said that the operator `+=` has precedence over the `--` because C# goes from left to right... (instead of C++ that goes from the right to the left) So the expression: ```csharp x += --x ``` Would be: ```csharp x = --(x + x) = --(43 + 43) = 85 ``` Nice... but weird :P

              C. Augusto Proiete https://augustoproiete.net

              J 1 Reply Last reply
              0
              • L leppie

                Nick Seng wrote: x += --x; == x = x + (--x) That should be: x--; (42) x = x + x; (84) leppie::AllocCPArticle(Generic DFA State Machine for .NET);

                B Offline
                B Offline
                Bo Hunter
                wrote on last edited by
                #7

                The decrement operator ( -- ) decrements its operand by 1. The decrement operator can appear before or after its operand: The first form is a prefix decrement operation. The result of the operation is the value of the operand after it has been decremented. The second form is a postfix decrement operation. The result of the operation is the value of the operand before it has been decremented. Bo Hunter

                1 Reply Last reply
                0
                • C C Augusto Proiete

                  Hi! I've posted this same question in the Microsoft C# Newsgroups and they said that the operator `+=` has precedence over the `--` because C# goes from left to right... (instead of C++ that goes from the right to the left) So the expression: ```csharp x += --x ``` Would be: ```csharp x = --(x + x) = --(43 + 43) = 85 ``` Nice... but weird :P

                  C. Augusto Proiete https://augustoproiete.net

                  J Offline
                  J Offline
                  jspano
                  wrote on last edited by
                  #8

                  Thanks for the info. I don't know if I agree with that though. Whether you eval from left to right or right to left, you still should evaluate in the proper precedence. The c# spec says that -- has precedence over +=. If you are interested it is in the spec section 7.2.1

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups