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. brain teaser...

brain teaser...

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

    The following code: int i = 0; cout << i++ << i; why does it output 00?

    T L D 3 Replies Last reply
    0
    • L Laffis

      The following code: int i = 0; cout << i++ << i; why does it output 00?

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      because i++ is the post-incrementation, which mean that the incrementation is done at the end of the expression (here cout << i++ << i;). by opposite, cout << ++i << i; would have outputed 11 and cout << i << ++i; would have outputed 01. is that all clear ?


      TOXCCT >>> GEII power
      [toxcct][VisualCalc]

      1 Reply Last reply
      0
      • L Laffis

        The following code: int i = 0; cout << i++ << i; why does it output 00?

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

        It does not. In my compiler it outputs 01 and that is correct .


        "Success is the ability to go from one failure to another with no loss of enthusiasm." - W.Churchill

        L 1 Reply Last reply
        0
        • L Lost User

          It does not. In my compiler it outputs 01 and that is correct .


          "Success is the ability to go from one failure to another with no loss of enthusiasm." - W.Churchill

          L Offline
          L Offline
          Laffis
          wrote on last edited by
          #4

          :-D I am sure this will catch someone out there, me included. The end result will be machine specific, ie. undefined. I guess it is because different machine will prioritise ++ and << in different manner.

          T 1 Reply Last reply
          0
          • L Laffis

            The following code: int i = 0; cout << i++ << i; why does it output 00?

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

            Laffis wrote: why does it output 00? Because it just happened to. "Sequence points" are those placecs at the end of statements, full expressions, before a function is invoked, and right after it returns. In between sequence points, side-effects of operators may not have taken place, and values are indeterminate. The exact point at which that increment takes place is unknown and unspecified; only the sequence-point semantics need to be satisfied. This allows compilers to either increment in-place after makng a copy or make an incremented copy which does not have to be set until the value is next needed, which by definition is after the sequence point.


            "One must learn from the bite of the fire to leave it alone." - Native American Proverb

            L 1 Reply Last reply
            0
            • L Laffis

              :-D I am sure this will catch someone out there, me included. The end result will be machine specific, ie. undefined. I guess it is because different machine will prioritise ++ and << in different manner.

              T Offline
              T Offline
              toxcct
              wrote on last edited by
              #6

              operators priority are not system dependant. the priority is defined by the compiler, and specified in the standard of C++ !!!


              TOXCCT >>> GEII power
              [toxcct][VisualCalc]

              T 1 Reply Last reply
              0
              • D David Crow

                Laffis wrote: why does it output 00? Because it just happened to. "Sequence points" are those placecs at the end of statements, full expressions, before a function is invoked, and right after it returns. In between sequence points, side-effects of operators may not have taken place, and values are indeterminate. The exact point at which that increment takes place is unknown and unspecified; only the sequence-point semantics need to be satisfied. This allows compilers to either increment in-place after makng a copy or make an incremented copy which does not have to be set until the value is next needed, which by definition is after the sequence point.


                "One must learn from the bite of the fire to leave it alone." - Native American Proverb

                L Offline
                L Offline
                Laffis
                wrote on last edited by
                #7

                Hmmmm, pretty good! mate...Beer beer...

                1 Reply Last reply
                0
                • T toxcct

                  operators priority are not system dependant. the priority is defined by the compiler, and specified in the standard of C++ !!!


                  TOXCCT >>> GEII power
                  [toxcct][VisualCalc]

                  T Offline
                  T Offline
                  toxcct
                  wrote on last edited by
                  #8

                  ...and I please the one who voted me down to explain his choice... :mad:


                  TOXCCT >>> GEII power
                  [toxcct][VisualCalc]

                  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