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. C pre increment and post increment

C pre increment and post increment

Scheduled Pinned Locked Moved C / C++ / MFC
question
8 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.
  • A Offline
    A Offline
    Anonygeeker
    wrote on last edited by
    #1

    Hi, int x=11; printf("%d %d %d ", x++,++x,++x); It prints 13 14 14, But I thought 13 14 15 Why ?

    L J D 3 Replies Last reply
    0
    • A Anonygeeker

      Hi, int x=11; printf("%d %d %d ", x++,++x,++x); It prints 13 14 14, But I thought 13 14 15 Why ?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Because you should never use such expressions. The compiler writers are allowed to handle the order of incrementation in any way they like, so the results may or may not be what you expect. Bottom line: don't do it.

      A 1 Reply Last reply
      0
      • A Anonygeeker

        Hi, int x=11; printf("%d %d %d ", x++,++x,++x); It prints 13 14 14, But I thought 13 14 15 Why ?

        J Offline
        J Offline
        Jochen Arndt
        wrote on last edited by
        #3

        It is executed in this order here:

        // Pre-increment is executed first
        ++x; // 12
        ++x; // 13
        // push first parameter on the stack: 13
        push x;
        // post increment
        x++; // 14
        // push 2nd and 3rd parameter on the stack: 14
        push x;
        push x;

        But see also Why does x = ++x + x++ give me the wrong answer?[^]

        A 1 Reply Last reply
        0
        • J Jochen Arndt

          It is executed in this order here:

          // Pre-increment is executed first
          ++x; // 12
          ++x; // 13
          // push first parameter on the stack: 13
          push x;
          // post increment
          x++; // 14
          // push 2nd and 3rd parameter on the stack: 14
          push x;
          push x;

          But see also Why does x = ++x + x++ give me the wrong answer?[^]

          A Offline
          A Offline
          Anonygeeker
          wrote on last edited by
          #4

          Good explanation..Thanks

          L 1 Reply Last reply
          0
          • L Lost User

            Because you should never use such expressions. The compiler writers are allowed to handle the order of incrementation in any way they like, so the results may or may not be what you expect. Bottom line: don't do it.

            A Offline
            A Offline
            Anonygeeker
            wrote on last edited by
            #5

            I agree. but my doubt was how it happens.

            1 Reply Last reply
            0
            • A Anonygeeker

              Hi, int x=11; printf("%d %d %d ", x++,++x,++x); It prints 13 14 14, But I thought 13 14 15 Why ?

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

              This expression is undefined by the C Standard, meaning that the compiler can do anything with it, including formatting your disk or causing demons to fly out of your nose. You should consider yourself lucky that the compiler only incremented 'x' in a weird manner. :) Note that even changing the compiler options (e.g. optimization) might change the printed results, so even if this "appears to work properly" on your system - DON'T DO IT! (On comp.std.c and comp.lang.c they used to refer to a notional system, the DeathStation 9000, that would actually create Nasal Demons when it encountered undefined expressions :) )

              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

              A 1 Reply Last reply
              0
              • A Anonygeeker

                Good explanation..Thanks

                L Offline
                L Offline
                leon de boer
                wrote on last edited by
                #7

                It isn't a good answer it's wrong the result is undefined. Try it on VS2017 and you will likely get "13 13 13 " because of the way the optimizer works.

                In vino veritas

                1 Reply Last reply
                0
                • D Daniel Pfeffer

                  This expression is undefined by the C Standard, meaning that the compiler can do anything with it, including formatting your disk or causing demons to fly out of your nose. You should consider yourself lucky that the compiler only incremented 'x' in a weird manner. :) Note that even changing the compiler options (e.g. optimization) might change the printed results, so even if this "appears to work properly" on your system - DON'T DO IT! (On comp.std.c and comp.lang.c they used to refer to a notional system, the DeathStation 9000, that would actually create Nasal Demons when it encountered undefined expressions :) )

                  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

                  A Offline
                  A Offline
                  Anonygeeker
                  wrote on last edited by
                  #8

                  thank you .. :)

                  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