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. --i not equal to i - 1 ???

--i not equal to i - 1 ???

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

    In the following function replacing "number - 1" does not yield the same correct result as "--number"? Why is that so? Am I'm missing something? int sum( int number ) { int result; if (number == 1) return number; else { result = sum(number - 1) + number; return result; } }

    P 1 Reply Last reply
    0
    • C Cristoff

      In the following function replacing "number - 1" does not yield the same correct result as "--number"? Why is that so? Am I'm missing something? int sum( int number ) { int result; if (number == 1) return number; else { result = sum(number - 1) + number; return result; } }

      P Offline
      P Offline
      PJ Arends
      wrote on last edited by
      #2

      --number changes the value of number, number - 1 does not. say number is 10: sum(number - 1) + number; is sum(9) + 10; and sum(--number) + number; is sum(9) + 9;


      You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!

      C P 2 Replies Last reply
      0
      • P PJ Arends

        --number changes the value of number, number - 1 does not. say number is 10: sum(number - 1) + number; is sum(9) + 10; and sum(--number) + number; is sum(9) + 9;


        You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!

        C Offline
        C Offline
        Cristoff
        wrote on last edited by
        #3

        The first line was enough! Thank you very much! Sometimes, well maybe most of the time I'm so stupid. That's so obvious!

        1 Reply Last reply
        0
        • P PJ Arends

          --number changes the value of number, number - 1 does not. say number is 10: sum(number - 1) + number; is sum(9) + 10; and sum(--number) + number; is sum(9) + 9;


          You may be right I may be crazy -- Billy Joel -- Within you lies the power for good - Use it!

          P Offline
          P Offline
          Peter_in_2780
          wrote on last edited by
          #4

          Being a bit pedantic here (but it's necessary!)... Since the order of evaluation is not defined by the language spec, the compiler writer is free to choose, so your second example sum(--number) + number; could come out either way (but number will always wind up decremented). There are lots of examples of confusion over this. Cheers, Peter [edit]formatting[/edit]

          Software rusts. Simon Stephenson, ca 1994.

          L 1 Reply Last reply
          0
          • P Peter_in_2780

            Being a bit pedantic here (but it's necessary!)... Since the order of evaluation is not defined by the language spec, the compiler writer is free to choose, so your second example sum(--number) + number; could come out either way (but number will always wind up decremented). There are lots of examples of confusion over this. Cheers, Peter [edit]formatting[/edit]

            Software rusts. Simon Stephenson, ca 1994.

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            I concur, at least for C. I just don't know for recent versions of C++. And I would never write a statement like that. :)

            Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

            S 1 Reply Last reply
            0
            • L Luc Pattyn

              I concur, at least for C. I just don't know for recent versions of C++. And I would never write a statement like that. :)

              Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

              Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

              S Offline
              S Offline
              Stefan_Lang
              wrote on last edited by
              #6

              According to this list, item 31 (on the table of contents), it is true for C++ as well.

              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