output of the code cout<<a+ a++ + --a; comes out to be 16 when a=5 what is the logic?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • umangbansal
    New Member
    • Jul 2010
    • 8

    #1

    output of the code cout<<a+ a++ + --a; comes out to be 16 when a=5 what is the logic?

    int a=5;
    cout<< a+ a++ + --a ;
    output is
    16
    logic behind the output??
  • umangbansal
    New Member
    • Jul 2010
    • 8

    #2
    Originally posted by umangbansal
    int a=5;
    cout<< a+ a++ + --a ;
    output is
    16
    logic behind the output??
    somebody reply please..

    Comment

    • code green
      Recognized Expert Top Contributor
      • Mar 2007
      • 1726

      #3
      I'm never comfortable with ugly looking code but it looks fairly simple to me
      Code:
      a=5
      cout 5 ... 5
      a++
      a=6
      cout 5 + 6 ... 11
      a--
      a=5
      cout 5 + 6 + 5... 16

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        This code:

        Code:
        cout<< a+ a++ + --a ;
        has indeterminate results.

        The compiler is free to evaluate variables in any order within a sequence point (a statement). Therefore, if a variable is changed more than once within a statement you cannot be sure of the result.

        Comment

        • umangbansal
          New Member
          • Jul 2010
          • 8

          #5
          Originally posted by weaknessforcats
          This code:

          Code:
          cout<< a+ a++ + --a ;
          has indeterminate results.

          The compiler is free to evaluate variables in any order within a sequence point (a statement). Therefore, if a variable is changed more than once within a statement you cannot be sure of the result.
          but this code always gives the same result..

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Are you using the same compiler?

            Visual C++ 2008 gives 12 as a result.

            As I said, the results are indeterminate.

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Originally posted by umangbansal;
              but this code always gives the same result..
              Accessing a variable more than once between sequence points where one of those accesses changes the variables value is undefined behaviour (as defined by the standard).

              A program exhibiting undefined behaviour may do anything including but not limited to
              • Exit without explanation
              • Produce some sort of exception
              • Appear to operate correctly
              • Operate correctly the whole time you are testing it and only go wrong the first time it is critically important that it works
              • Format your hard drive
              • Make demons fly out of your nose


              The compiler is under no constraints with the code it produces once undefined behaviour is invoked.

              Trying to determine the output of a program exhibiting undefined behaviour is pointless because it is under no constraint to stick to whatever assumptions you have made while making that determination.

              Your only real solution is to not write code that exhibits undefined behaviour.


              I am beginning to feel like a scratched record on the subject a quick forum search would have thrown up several recent threads on this very subject.

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #8
                banfa: "I am beginning to feel like a scratched record on the subject ..."

                You mean a broken record, as in one where the stylus skips backwards and repeats the same audio over and over again.

                It is true that a scratch might produce a backwards skip. However, in today's lingo, scratch refers to the effect made when a record is rotated back and forth through a small angle.

                You're must be too young to remember vinyl records, but too old to be familiar with rap. ;-)

                Comment

                • Dheeraj Joshi
                  Recognized Expert Top Contributor
                  • Jul 2009
                  • 1129

                  #9
                  Make demons fly out of your nose
                  Again demons started to fly. lol.

                  Regards
                  Dheeraj Joshi

                  Comment

                  • Banfa
                    Recognized Expert Expert
                    • Feb 2006
                    • 9067

                    #10
                    Originally posted by donbock;
                    You're must be too young to remember vinyl records, but too old to be familiar with rap. ;-)
                    I think that would be a very narrow time frame. I do remember and in fact did own vinyl I just got my metaphors mixed :D

                    Comment

                    Working...