operator ++ in C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ThuKhanh
    New Member
    • Apr 2007
    • 7

    operator ++ in C++

    Please help me explain this .
    I' m using MS Visual C++6.0 . First i take the initialization :int i=5;
    >if i use printf("%d %d %d",i++,i++,i++ ) ;the output is 5 5 5
    >if i use cout<<i++<<i++< <i++; the output is 7 6 5 !
    It's so confusing ! Due to the residual -value law of the post increment operator i think the output must be 5 6 7 ! Why 7 6 5 - a reverse order ?
    And more , printf and cout are different , but I wonder why ?
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Originally posted by ThuKhanh
    Please help me explain this .
    I' m using MS Visual C++6.0 . First i take the initialization :int i=5;
    >if i use printf("%d %d %d",i++,i++,i++ ) ;the output is 5 5 5
    >if i use cout<<i++<<i++< <i++; the output is 7 6 5 !
    It's so confusing ! Due to the residual -value law of the post increment operator i think the output must be 5 6 7 ! Why 7 6 5 - a reverse order ?
    And more , printf and cout are different , but I wonder why ?
    I'm not entirely sure on this, but I'd guess that it's because printf has parentheses - it's interpreted as a single statement, the arguments are not taken all at once - they're dealt with as they are given to cout, so the first one is done, incremented, and then the second one is taken, and after printed, is incremented, etc...

    Comment

    • sicarie
      Recognized Expert Specialist
      • Nov 2006
      • 4677

      #3
      Actually, I just ran that in Cygwin, and it gave me (with i initialized as 1):

      1 2 3
      4 5 6

      I think that might be something with the VC++ compiler...
      Last edited by sicarie; Apr 12 '07, 03:27 AM. Reason: Asked a dumb question the OP had included in his post.

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Originally posted by sicarie
        Actually, I just ran that in Cygwin, and it gave me (with i initialized as 1):

        1 2 3
        4 5 6

        I think that might be something with the VC++ compiler...
        Yep, just ran it on Ubuntu and got

        3 2 1 (I did cout first, though)
        4 5 6 (and this was the printf line)

        Comment

        • ThuKhanh
          New Member
          • Apr 2007
          • 7

          #5
          Thank you for answering .
          But i'm still confused .Because when i write printf("%d %d %d",++i,++i,++i ) the result is 8 7 6 ( not 6 7 8 !)

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            Originally posted by ThuKhanh
            Thank you for answering .
            But i'm still confused .Because when i write printf("%d %d %d",++i,++i,++i ) the result is 8 7 6 ( not 6 7 8 !)
            I'm pretty sure it's compiler and platform dependent - it depends on how the people who wrote your OS, and then the people who wrote your compiler for your OS, decided to do it. It's really bizzare to me that they print out backwards.

            Comment

            • nmadct
              Recognized Expert New Member
              • Jan 2007
              • 83

              #7
              Surprising but easily explained, it would seem that the VC grammar defines argument lists in a right-recursive manner so that the rightmost term is the innermost level of the syntax tree and is evaluated first.

              Comment

              • sicarie
                Recognized Expert Specialist
                • Nov 2006
                • 4677

                #8
                Originally posted by nmadct
                Surprising but easily explained, it would seem that the VC grammar defines argument lists in a right-recursive manner so that the rightmost term is the innermost level of the syntax tree and is evaluated first.
                Seems GCC is that way as well, thanks nmadct!

                Comment

                • gagandeepgupta16
                  New Member
                  • Feb 2007
                  • 56

                  #9
                  hi

                  Always the statement is executed from right to left order, and in printf() you used i++ that says first use then increment, and since the parameter are instantiated at same instance hence all three values of i were taken as 5 if you use another statement there you will find the incremented value for i.

                  as for cout it passes as individual recursive call from right to left hence first the rightmost value is passed i.e. i++ this will return 5 but the actual value will b incremented. then second right most value i++ will use the incremented value i.e 6 and so the third right most value will take it 7 hence the output will be - 7 6 5.

                  hope this explained you.

                  cheers


                  Originally posted by ThuKhanh
                  Please help me explain this .
                  I' m using MS Visual C++6.0 . First i take the initialization :int i=5;
                  >if i use printf("%d %d %d",i++,i++,i++ ) ;the output is 5 5 5
                  >if i use cout<<i++<<i++< <i++; the output is 7 6 5 !
                  It's so confusing ! Due to the residual -value law of the post increment operator i think the output must be 5 6 7 ! Why 7 6 5 - a reverse order ?
                  And more , printf and cout are different , but I wonder why ?

                  Comment

                  • ThuKhanh
                    New Member
                    • Apr 2007
                    • 7

                    #10
                    But the compiler only reads from the right with integers ?
                    for ex , i write int i=5; cout<<"U "<<++i<<"ha ve "<<++i;
                    it displays "U 7 have 6" .So the compiler works in such a funny way :go through ,write all strings and steps backward with int ! I think you are right , but it's funny !

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      When a modifiable lhv (Left Hand Value) is modified more than once before a
                      sequence point is reached the behaviour will be undedined. An expression such
                      as cout << i++ << i++ << i++ does just that: modifying a lhv more than once
                      before a sequence point is reached and thus induces undefined behaviour.

                      No more can be said about it.

                      kind regards,

                      Jos

                      Comment

                      • nmadct
                        Recognized Expert New Member
                        • Jan 2007
                        • 83

                        #12
                        The behavior is undefined by the standard, as Jos said, but obviously a particular compiler implementation defines a behavior, whatever that may be. It might be fun to poke around and figure out what that behavior is. You should never write any program that depends on that behavior though. Even if you are targeting only one particular compiler and you know how it behaves regarding this issue, there's no guarantee that it won't change tomorrow. (In the case of GCC we can know exactly what it does if we care to, because we have the source code.)

                        Comment

                        • weaknessforcats
                          Recognized Expert Expert
                          • Mar 2007
                          • 9214

                          #13
                          The correct sequence to display is 7 6 5.

                          This code:

                          cout<<i++<<i++< <i++;

                          is really:

                          operator<<((ope rator<<(cout, i++), i++), i++);

                          where i is 5 6 7

                          That is, a nested call where function calls are used as function arguments.

                          The outer call is made first, Hence 7.
                          To make next inner call you need to call the left argument. i will be 6
                          To make the inner call you need to call the inner inner left argument. i will be 5.

                          Using printf(), you get the same results.

                          Comment

                          • JosAH
                            Recognized Expert MVP
                            • Mar 2007
                            • 11453

                            #14
                            Originally posted by weaknessforcats
                            The correct sequence to display is 7 6 5.
                            Nope, no matter what clever sophistic reasoning you apply, the expression
                            results in undefined behaviour for reasons I wrote in a previous reply. Maybe
                            sad but true. btw, not even the order of parameter evaluation is specified so
                            that is another problem w.r.t. undefined behaviour.

                            kind regards,

                            Jos

                            Comment

                            Working...