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 / C++ / MFC
  4. why 4 twice

why 4 twice

Scheduled Pinned Locked Moved C / C++ / MFC
question
7 Posts 6 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.
  • U Offline
    U Offline
    User 11710213
    wrote on last edited by
    #1

    #include int main(void) { int x=2; printf("%d\n%d\n",x++,x++); } why the result is 4 4 not 3 4 ?!

    D K F D 4 Replies Last reply
    0
    • U User 11710213

      #include int main(void) { int x=2; printf("%d\n%d\n",x++,x++); } why the result is 4 4 not 3 4 ?!

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

      Google for "order of evaluations." It's a much-talked-about subject.

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

      1 Reply Last reply
      0
      • U User 11710213

        #include int main(void) { int x=2; printf("%d\n%d\n",x++,x++); } why the result is 4 4 not 3 4 ?!

        K Offline
        K Offline
        k5054
        wrote on last edited by
        #3

        The order of evaluation of the two post increment operations (x++) is not guaranteed. Using gcc I get 3, 2, using clang I get 2, 3. Both are valid answers, as are would be any combination of 2, 3 and 4. In fact, it would not be a surprise if different levels of optimization produced different results. I think the only thing that can be guaranteed is that the value of x will be 4 after the call to printf().

        1 Reply Last reply
        0
        • U User 11710213

          #include int main(void) { int x=2; printf("%d\n%d\n",x++,x++); } why the result is 4 4 not 3 4 ?!

          F Offline
          F Offline
          Frankie C
          wrote on last edited by
          #4

          You got this result because the optimizations are on. The compiler choose an optimization that leads to the final result not considering what the values could be in the same call. This is consistent with C99, C11 and C++11 standards that to privilege the best optimization of code state that the order of evaluation of the parameters of a function is undefined, and compiler dependent (but also situation dependent different conditions of optimization will lead to different results). If you test your code with different compilers you'll get different results. Please consider that even with optimizations off the compiler could give 'strange' values. BTW the more common output you should expect should be '4 3', because the parameters should normally be evaluated from right to left (incrementing 2 to 3 and pushing it on the stack, then incrementing it to 4 and pushing it on the stack). When optimization are on basically the compiler decides to make a double increment in one time (instead of move back to the same variable for a second increment).

          1 Reply Last reply
          0
          • U User 11710213

            #include int main(void) { int x=2; printf("%d\n%d\n",x++,x++); } why the result is 4 4 not 3 4 ?!

            D Offline
            D Offline
            Daniel Pfeffer
            wrote on last edited by
            #5

            The code produces undefined behavior - you are modifying an 'lvalue' twice between 'sequence points'. Undefined behavior means precisely that; the compiler is at liberty to generate code that will format your disk, or even make demons fly out of your nose. :) Be thankful that all it did was give you an unexpected result.

            If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill

            S 1 Reply Last reply
            0
            • D Daniel Pfeffer

              The code produces undefined behavior - you are modifying an 'lvalue' twice between 'sequence points'. Undefined behavior means precisely that; the compiler is at liberty to generate code that will format your disk, or even make demons fly out of your nose. :) Be thankful that all it did was give you an unexpected result.

              If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill

              S Offline
              S Offline
              Stefan_Lang
              wrote on last edited by
              #6

              Hmm, I never had demons fly out of my nose - I think I need to try harder! :-D

              GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

              D 1 Reply Last reply
              0
              • S Stefan_Lang

                Hmm, I never had demons fly out of my nose - I think I need to try harder! :-D

                GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                D Offline
                D Offline
                Daniel Pfeffer
                wrote on last edited by
                #7

                One of the regulars on the newsgroup comp.lang.c invented a notional system - the DeathStation 9000. The C compiler for that system would interpret undefined and implementation-defined behavior in the worst possible manner, up to and including creation of nasal demons...

                If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill

                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