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#
  4. Hey folks, that's funny...

Hey folks, that's funny...

Scheduled Pinned Locked Moved C#
csharpc++javaquestion
8 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.
  • A Offline
    A Offline
    Alexander Zhevak
    wrote on last edited by
    #1

    there is next code: int a; int b; a = 5; b = ++a + ++a; // Can you guess what will be the "b"? Try it compile and run in various languages: C++ and C# and Java. I had got different results even in the same compiler (Watcom C++) -- with and without optimisation.

    A

    C 1 Reply Last reply
    0
    • A Alexander Zhevak

      there is next code: int a; int b; a = 5; b = ++a + ++a; // Can you guess what will be the "b"? Try it compile and run in various languages: C++ and C# and Java. I had got different results even in the same compiler (Watcom C++) -- with and without optimisation.

      A

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      ++a is the prefix incrementor a++ is the postfix incrementor if a = 5 then ++a returns 6 and a becomes 6 if a = 5 then a++ returns 5 and a becomes 6 therefore

      b = ++a + ++a
      = 6 + 7
      = 13


      Upcoming events: * Glasgow: SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

      A 1 Reply Last reply
      0
      • C Colin Angus Mackay

        ++a is the prefix incrementor a++ is the postfix incrementor if a = 5 then ++a returns 6 and a becomes 6 if a = 5 then a++ returns 5 and a becomes 6 therefore

        b = ++a + ++a
        = 6 + 7
        = 13


        Upcoming events: * Glasgow: SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website

        A Offline
        A Offline
        andre_swnpl
        wrote on last edited by
        #3

        You are correct and this is what i got with c# as well, but c++ gave me 14.

        A T 2 Replies Last reply
        0
        • A andre_swnpl

          You are correct and this is what i got with c# as well, but c++ gave me 14.

          A Offline
          A Offline
          Arun Immanuel
          wrote on last edited by
          #4

          I have read this statement in a book. "The results of expressions which use same variables with increment operators more than once are unpredicatble in C Language" int i=0; i=i++ + ++i; //3 not 2 ----------------- int i=0,j=0; j=i++ + ++i; //2 as esxpected You can try with lot of examples...

          Regards, Arun Kumar.A

          A 1 Reply Last reply
          0
          • A Arun Immanuel

            I have read this statement in a book. "The results of expressions which use same variables with increment operators more than once are unpredicatble in C Language" int i=0; i=i++ + ++i; //3 not 2 ----------------- int i=0,j=0; j=i++ + ++i; //2 as esxpected You can try with lot of examples...

            Regards, Arun Kumar.A

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

            It would indeed seem that way. If I do the following in c++ I do get the expected result int a; int b; a = 5; b = ++a; b += ++a;

            A 1 Reply Last reply
            0
            • A andre_swnpl

              It would indeed seem that way. If I do the following in c++ I do get the expected result int a; int b; a = 5; b = ++a; b += ++a;

              A Offline
              A Offline
              Arun Immanuel
              wrote on last edited by
              #6

              Try to use the variable 'a' more than once(with increment/decrement operator) in a single expression and assign the result to 'a' itself. You will get unexpected results.

              Regards, Arun Kumar.A

              A 1 Reply Last reply
              0
              • A andre_swnpl

                You are correct and this is what i got with c# as well, but c++ gave me 14.

                T Offline
                T Offline
                Tim Paaschen
                wrote on last edited by
                #7

                In C++ this expression results in undefined behavior. Look here[^] for details.

                Regards, Tim

                1 Reply Last reply
                0
                • A Arun Immanuel

                  Try to use the variable 'a' more than once(with increment/decrement operator) in a single expression and assign the result to 'a' itself. You will get unexpected results.

                  Regards, Arun Kumar.A

                  A Offline
                  A Offline
                  Alexander Zhevak
                  wrote on last edited by
                  #8

                  However. It's strange, C++ gives 14 at the same time C# gives 13. The different interpretation of statements?

                  A

                  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