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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Different Output

Different Output

Scheduled Pinned Locked Moved C / C++ / MFC
csharphelp
9 Posts 5 Posters 1 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.
  • R Offline
    R Offline
    rajeevktripathi
    wrote on last edited by
    #1

    Hi All I have been puzzelled, actually problem is that I run a code in C# project and in Console based project of Visual Stdio 2005. code is as follows: int i = 1; int j = (++i) + (++i) + (++i); Now I found different values for j in C# project( j = 9) and console based project( j= 12). So please suggest me the answer behind this different values of j in both projects. Thansk & Regards

    M 1 Reply Last reply
    0
    • R rajeevktripathi

      Hi All I have been puzzelled, actually problem is that I run a code in C# project and in Console based project of Visual Stdio 2005. code is as follows: int i = 1; int j = (++i) + (++i) + (++i); Now I found different values for j in C# project( j = 9) and console based project( j= 12). So please suggest me the answer behind this different values of j in both projects. Thansk & Regards

      M Offline
      M Offline
      Mike Dimmick
      wrote on last edited by
      #2

      C++ does not guarantee an evaluation order for operations within an expression. The compiler is free to, as in this case, perform all three preincrement operations first and then sum the results. Sometimes this gives the compiler greater opportunity for optimization. The solution is not to preincrement or postincrement a variable multiple times in the same expression. C# guarantees left-to-right evaluation order, so the result is always 2 + 3 + 4 = 9.

      Stability. What an interesting concept. -- Chris Maunder

      J 1 Reply Last reply
      0
      • M Mike Dimmick

        C++ does not guarantee an evaluation order for operations within an expression. The compiler is free to, as in this case, perform all three preincrement operations first and then sum the results. Sometimes this gives the compiler greater opportunity for optimization. The solution is not to preincrement or postincrement a variable multiple times in the same expression. C# guarantees left-to-right evaluation order, so the result is always 2 + 3 + 4 = 9.

        Stability. What an interesting concept. -- Chris Maunder

        J Offline
        J Offline
        John R Shaw
        wrote on last edited by
        #3

        True, but the answer should still be 9 whether left to right or right to left or any other order. Where is that 12 coming from? Hmmm…:doh:

        INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

        I 1 Reply Last reply
        0
        • J John R Shaw

          True, but the answer should still be 9 whether left to right or right to left or any other order. Where is that 12 coming from? Hmmm…:doh:

          INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

          I Offline
          I Offline
          iddqd515
          wrote on last edited by
          #4

          It was just explained. The compiler can evaluate all 3 ++ operations before performing the rest of the arithmetic. Thus you end up with i++, i++, i++ so i = 4. Now j = 4 + 4 + 4 = 12. I'm not that familiar with C# and its compiler so I can't speak for why Console apps in C# do it. But it certainly makes sense in a C++ context.

          J 1 Reply Last reply
          0
          • I iddqd515

            It was just explained. The compiler can evaluate all 3 ++ operations before performing the rest of the arithmetic. Thus you end up with i++, i++, i++ so i = 4. Now j = 4 + 4 + 4 = 12. I'm not that familiar with C# and its compiler so I can't speak for why Console apps in C# do it. But it certainly makes sense in a C++ context.

            J Offline
            J Offline
            John R Shaw
            wrote on last edited by
            #5

            Actually it makes no sense in a C++ context, unless the compiler is flawed. But I do see what the problem is now. I do not write code in the form presented for reasons like that, but a compiler should know how to handle it correctly (even with optimization). I suspect that the standard leaves this possibility open, but logic should prevail where the standard fails. By the way there is a major difference between i++ and ++i (in code) in C++; C is more forgiving, but not with integral types.

            INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

            I D 2 Replies Last reply
            0
            • J John R Shaw

              Actually it makes no sense in a C++ context, unless the compiler is flawed. But I do see what the problem is now. I do not write code in the form presented for reasons like that, but a compiler should know how to handle it correctly (even with optimization). I suspect that the standard leaves this possibility open, but logic should prevail where the standard fails. By the way there is a major difference between i++ and ++i (in code) in C++; C is more forgiving, but not with integral types.

              INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

              I Offline
              I Offline
              iddqd515
              wrote on last edited by
              #6

              Presumably getting such a thing working is just extra work to enable horribly confusing code. I'm pretty content to let the compiler discourage people from writing statements filled with pre and post increments nested in other operations.

              J 1 Reply Last reply
              0
              • I iddqd515

                Presumably getting such a thing working is just extra work to enable horribly confusing code. I'm pretty content to let the compiler discourage people from writing statements filled with pre and post increments nested in other operations.

                J Offline
                J Offline
                John R Shaw
                wrote on last edited by
                #7

                I agree; it is just that the code presented was not ‘horribly confusing’. I have seen ‘horribly confusing code’ and that did not even come close. Now take away the parenthesis and the compiler will interpret it the same, but the viewer would have to stop and think about it. Oh well, its does not matter to me because I don’t do that. Thanks for you explanation of how the 12 came about.

                INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

                1 Reply Last reply
                0
                • J John R Shaw

                  Actually it makes no sense in a C++ context, unless the compiler is flawed. But I do see what the problem is now. I do not write code in the form presented for reasons like that, but a compiler should know how to handle it correctly (even with optimization). I suspect that the standard leaves this possibility open, but logic should prevail where the standard fails. By the way there is a major difference between i++ and ++i (in code) in C++; C is more forgiving, but not with integral types.

                  INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  John R. Shaw wrote:

                  By the way there is a major difference between i++ and ++i...

                  How so?


                  "A good athlete is the result of a good and worthy opponent." - David Crow

                  "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                  J 1 Reply Last reply
                  0
                  • D David Crow

                    John R. Shaw wrote:

                    By the way there is a major difference between i++ and ++i...

                    How so?


                    "A good athlete is the result of a good and worthy opponent." - David Crow

                    "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                    J Offline
                    J Offline
                    John R Shaw
                    wrote on last edited by
                    #9

                    For one, in the original question ‘j’ would have been equal to 3. Second if ‘i’ was a class instead of an ‘int’, then the overhead could be expensive in C++. But you already know that. Always prefer ‘++i’ to ‘i++’. Just add support for ‘++’ (pre & post) to any class and you will see the problem, the compiler does not optimize that away.

                    INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

                    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