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. Wrong evaluation of pre- and post-increment on variables: What is wrong here?!?!

Wrong evaluation of pre- and post-increment on variables: What is wrong here?!?!

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

    I tried the following in VS6.0. Created a simple "Hello World" application then added the following:

    int i;
    int j;
    i = j = 5;

    printf("%d %d %d %d %d %d\n",++i,j++,i++,++j,++i,j++);
    printf("i %d j %d\n",i,j);

    Now i expected the output to be: 8 7 6 7 6 5 i 8 j 8 But instead it printed as: 7 6 6 6 6 5 i 8 j 8 :confused::confused::confused:

    CPalliniC S D 3 Replies Last reply
    0
    • _ __yash__

      I tried the following in VS6.0. Created a simple "Hello World" application then added the following:

      int i;
      int j;
      i = j = 5;

      printf("%d %d %d %d %d %d\n",++i,j++,i++,++j,++i,j++);
      printf("i %d j %d\n",i,j);

      Now i expected the output to be: 8 7 6 7 6 5 i 8 j 8 But instead it printed as: 7 6 6 6 6 5 i 8 j 8 :confused::confused::confused:

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      Ralph_2 wrote:

      Now i expected the output to be: 8 7 6 7 6 5

      You cannot expect that behaviour (and you should avoid using the increment operators that way). :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      In testa che avete, signor di Ceprano?

      _ 1 Reply Last reply
      0
      • CPalliniC CPallini

        Ralph_2 wrote:

        Now i expected the output to be: 8 7 6 7 6 5

        You cannot expect that behaviour (and you should avoid using the increment operators that way). :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        _ Offline
        _ Offline
        __yash__
        wrote on last edited by
        #3

        CPallini wrote:

        and you should avoid using the increment operators that way

        Oh, this is just for trying...I don't use it any where as such

        CPallini wrote:

        You cannot expect that behaviour

        Why cannot i expect the output to be that? You think the output is wrong? Or different compilers will treat the statement differently??

        CPalliniC D 2 Replies Last reply
        0
        • _ __yash__

          CPallini wrote:

          and you should avoid using the increment operators that way

          Oh, this is just for trying...I don't use it any where as such

          CPallini wrote:

          You cannot expect that behaviour

          Why cannot i expect the output to be that? You think the output is wrong? Or different compilers will treat the statement differently??

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          Ralph_2 wrote:

          Or different compilers will treat the statement differently??

          Yes. :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          In testa che avete, signor di Ceprano?

          _ 1 Reply Last reply
          0
          • _ __yash__

            I tried the following in VS6.0. Created a simple "Hello World" application then added the following:

            int i;
            int j;
            i = j = 5;

            printf("%d %d %d %d %d %d\n",++i,j++,i++,++j,++i,j++);
            printf("i %d j %d\n",i,j);

            Now i expected the output to be: 8 7 6 7 6 5 i 8 j 8 But instead it printed as: 7 6 6 6 6 5 i 8 j 8 :confused::confused::confused:

            S Offline
            S Offline
            SandipG
            wrote on last edited by
            #5

            Behavior in this case depends on in which order the operands are pushed on the stack( Right to left or Left to right) when printf is called.

            Regards, Sandip.

            _ 1 Reply Last reply
            0
            • S SandipG

              Behavior in this case depends on in which order the operands are pushed on the stack( Right to left or Left to right) when printf is called.

              Regards, Sandip.

              _ Offline
              _ Offline
              __yash__
              wrote on last edited by
              #6

              True. But the Standard Calling Convention for C is right-to-left. The output 7 6 6 6 6 5 does not seem to be correct even if you were to pass the values left to right :)

              S 1 Reply Last reply
              0
              • CPalliniC CPallini

                Ralph_2 wrote:

                Or different compilers will treat the statement differently??

                Yes. :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                _ Offline
                _ Offline
                __yash__
                wrote on last edited by
                #7

                This actually sounds absurd to me. Its more like anti-C :doh:

                1 Reply Last reply
                0
                • _ __yash__

                  True. But the Standard Calling Convention for C is right-to-left. The output 7 6 6 6 6 5 does not seem to be correct even if you were to pass the values left to right :)

                  S Offline
                  S Offline
                  SandipG
                  wrote on last edited by
                  #8

                  I dont see anything wrong with the output? ++i,j++,i++,++j,++i,j++ 1. j++ = 5 2. ++i = 6 3. ++j = 6 4. i++ = 6 5. j++ = 6 7. ++i = 7 You can read more about it here Calling Conventions Demystified[^]

                  Regards, Sandip.

                  C _ 2 Replies Last reply
                  0
                  • S SandipG

                    I dont see anything wrong with the output? ++i,j++,i++,++j,++i,j++ 1. j++ = 5 2. ++i = 6 3. ++j = 6 4. i++ = 6 5. j++ = 6 7. ++i = 7 You can read more about it here Calling Conventions Demystified[^]

                    Regards, Sandip.

                    C Offline
                    C Offline
                    Covean
                    wrote on last edited by
                    #9

                    Your example is wrong! I dont see anything wrong with the output? ++i,j++,i++,++j,++i,j++ 1. j++ = 5 true -> j = 5 but after this statement j=6! 2. ++i = 6 true 3. ++j = 6 wrong -> j=7! 4. i++ = 6 true, but after this statement i=7! 5. j++ = 6 wrong -> j=7! 7. ++i = 7 wrong -> i = 8 So the final result is: 8 7 6 7 6 5 Thats also the right compiler result!

                    _ S 2 Replies Last reply
                    0
                    • S SandipG

                      I dont see anything wrong with the output? ++i,j++,i++,++j,++i,j++ 1. j++ = 5 2. ++i = 6 3. ++j = 6 4. i++ = 6 5. j++ = 6 7. ++i = 7 You can read more about it here Calling Conventions Demystified[^]

                      Regards, Sandip.

                      _ Offline
                      _ Offline
                      __yash__
                      wrote on last edited by
                      #10

                      I will surely read the article, i see a lot many interesting points there. But before that, just a little curious, 1. j++ = 5 //OK. Printed value is 5, after that it becomes 6 2. ++i = 6 //OK. First i is incremented to 6 then printed out 3. ++j = 6 //Incorrect.j will first be incremented then printed out. But note that present value of j is 6 (from step 1). So j at this stage becomes 7. 4. i++ = 6 //OK. i now is 7 after printing 5. j++ = 6 //Incorrect. Should have been 7 6. ++i = 7 //Incorrect. Should have been 8 Just to make things a little simpler

                      i = j = 5;
                      printf("%d %d %d\n",i++,i++,i++);
                      printf("i %d\n",i);

                      Now the output is: 5 5 5 i 8 Even if the beahviour is compiler-specific how do you interpret this example (or even the first one)?

                      S C 2 Replies Last reply
                      0
                      • C Covean

                        Your example is wrong! I dont see anything wrong with the output? ++i,j++,i++,++j,++i,j++ 1. j++ = 5 true -> j = 5 but after this statement j=6! 2. ++i = 6 true 3. ++j = 6 wrong -> j=7! 4. i++ = 6 true, but after this statement i=7! 5. j++ = 6 wrong -> j=7! 7. ++i = 7 wrong -> i = 8 So the final result is: 8 7 6 7 6 5 Thats also the right compiler result!

                        _ Offline
                        _ Offline
                        __yash__
                        wrote on last edited by
                        #11

                        I did not see your post before posting mine....i agree with you....the only viable answer so far seems to be that such evaluations are "compiler-specific" but even then I am not able to derive a logic for the output 5 6 6 6 6 7 !!! After all compilers are not like girl-friends :-\ there has to be some logic or rules that determine the output.

                        1 Reply Last reply
                        0
                        • C Covean

                          Your example is wrong! I dont see anything wrong with the output? ++i,j++,i++,++j,++i,j++ 1. j++ = 5 true -> j = 5 but after this statement j=6! 2. ++i = 6 true 3. ++j = 6 wrong -> j=7! 4. i++ = 6 true, but after this statement i=7! 5. j++ = 6 wrong -> j=7! 7. ++i = 7 wrong -> i = 8 So the final result is: 8 7 6 7 6 5 Thats also the right compiler result!

                          S Offline
                          S Offline
                          SandipG
                          wrote on last edited by
                          #12

                          Covean wrote:

                          Your example is wrong! I dont see anything wrong with the output? ++i,j++,i++,++j,++i,j++ 1. j++ = 5 true -> j = 5 but after this statement j=6!

                          Please Try following sample

                          int i=5;
                          printf("%d %d %d %d %d %d",i++,i++,i++,i++,i++,i++);

                          According to you what should be the output? 11 10 9 8 7 6 5? With VS 6.0 the output is 5 5 5 5 5 5 i++ is post increment and will be executed after execution of printf. Please correct me if i am wrong?

                          Regards, Sandip.

                          C 1 Reply Last reply
                          0
                          • _ __yash__

                            I will surely read the article, i see a lot many interesting points there. But before that, just a little curious, 1. j++ = 5 //OK. Printed value is 5, after that it becomes 6 2. ++i = 6 //OK. First i is incremented to 6 then printed out 3. ++j = 6 //Incorrect.j will first be incremented then printed out. But note that present value of j is 6 (from step 1). So j at this stage becomes 7. 4. i++ = 6 //OK. i now is 7 after printing 5. j++ = 6 //Incorrect. Should have been 7 6. ++i = 7 //Incorrect. Should have been 8 Just to make things a little simpler

                            i = j = 5;
                            printf("%d %d %d\n",i++,i++,i++);
                            printf("i %d\n",i);

                            Now the output is: 5 5 5 i 8 Even if the beahviour is compiler-specific how do you interpret this example (or even the first one)?

                            C Offline
                            C Offline
                            Covean
                            wrote on last edited by
                            #13

                            I cant believe the output: 5 5 5 I tried your code and it says "7 6 5". What is right. And the 8 7 6 7 6 5 from the prev. problem i also said that the compiler output is correct.

                            1 Reply Last reply
                            0
                            • _ __yash__

                              I will surely read the article, i see a lot many interesting points there. But before that, just a little curious, 1. j++ = 5 //OK. Printed value is 5, after that it becomes 6 2. ++i = 6 //OK. First i is incremented to 6 then printed out 3. ++j = 6 //Incorrect.j will first be incremented then printed out. But note that present value of j is 6 (from step 1). So j at this stage becomes 7. 4. i++ = 6 //OK. i now is 7 after printing 5. j++ = 6 //Incorrect. Should have been 7 6. ++i = 7 //Incorrect. Should have been 8 Just to make things a little simpler

                              i = j = 5;
                              printf("%d %d %d\n",i++,i++,i++);
                              printf("i %d\n",i);

                              Now the output is: 5 5 5 i 8 Even if the beahviour is compiler-specific how do you interpret this example (or even the first one)?

                              S Offline
                              S Offline
                              SandipG
                              wrote on last edited by
                              #14

                              Please check my other reply for answer.

                              Regards, Sandip.

                              1 Reply Last reply
                              0
                              • _ __yash__

                                CPallini wrote:

                                and you should avoid using the increment operators that way

                                Oh, this is just for trying...I don't use it any where as such

                                CPallini wrote:

                                You cannot expect that behaviour

                                Why cannot i expect the output to be that? You think the output is wrong? Or different compilers will treat the statement differently??

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

                                Ralph_2 wrote:

                                Or different compilers will treat the statement differently??

                                Yes. It's all about sequence points.

                                "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                                "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

                                1 Reply Last reply
                                0
                                • S SandipG

                                  Covean wrote:

                                  Your example is wrong! I dont see anything wrong with the output? ++i,j++,i++,++j,++i,j++ 1. j++ = 5 true -> j = 5 but after this statement j=6!

                                  Please Try following sample

                                  int i=5;
                                  printf("%d %d %d %d %d %d",i++,i++,i++,i++,i++,i++);

                                  According to you what should be the output? 11 10 9 8 7 6 5? With VS 6.0 the output is 5 5 5 5 5 5 i++ is post increment and will be executed after execution of printf. Please correct me if i am wrong?

                                  Regards, Sandip.

                                  C Offline
                                  C Offline
                                  Covean
                                  wrote on last edited by
                                  #16

                                  Can you please look at the asm-code your compiler generates cause my compiler answers your question in the correct way.

                                  1 Reply Last reply
                                  0
                                  • _ __yash__

                                    I tried the following in VS6.0. Created a simple "Hello World" application then added the following:

                                    int i;
                                    int j;
                                    i = j = 5;

                                    printf("%d %d %d %d %d %d\n",++i,j++,i++,++j,++i,j++);
                                    printf("i %d j %d\n",i,j);

                                    Now i expected the output to be: 8 7 6 7 6 5 i 8 j 8 But instead it printed as: 7 6 6 6 6 5 i 8 j 8 :confused::confused::confused:

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

                                    All of this "I expected..." and "it should be..." stuff is pointless. Compiler vendors are free to evaluate such expressions as they wish just so long as all side effects of previous evaluations have been performed at each sequence point. In your printf example, the order in which the arguments are evaluated is not specified, but the sequence point means that all of their side effects are complete before printf() is entered.

                                    "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                                    "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

                                    _ 1 Reply Last reply
                                    0
                                    • D David Crow

                                      All of this "I expected..." and "it should be..." stuff is pointless. Compiler vendors are free to evaluate such expressions as they wish just so long as all side effects of previous evaluations have been performed at each sequence point. In your printf example, the order in which the arguments are evaluated is not specified, but the sequence point means that all of their side effects are complete before printf() is entered.

                                      "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                                      "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

                                      _ Offline
                                      _ Offline
                                      __yash__
                                      wrote on last edited by
                                      #18

                                      DavidCrow wrote:

                                      All of this "I expected..." and "it should be..." stuff is pointless

                                      What I mean to imply here is that based on what I have read/know the output should have been so and so.....So may be my understanding of this concept (of calling convention, sequence points) needs to be brushed up....It *could be* my ignorance on the matter, lets see, I'll read more, do more research and see what's missing :)

                                      DavidCrow wrote:

                                      Compiler vendors are free to evaluate such expressions as they wish just so long as all side effects of previous evaluations have been performed at each sequence point.

                                      Expressions....true, but the point here is about "passing variables" and C does specify how to do that: either R-to-L or L-to-R, we can make that selection.

                                      DavidCrow wrote:

                                      In your printf example, the order in which the arguments are evaluated is not specified,

                                      I did not specify that but I m using the default calling convention under which variables are passed R-to-L

                                      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