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. Increment and Decrement Operators

Increment and Decrement Operators

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelptutorial
24 Posts 8 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.
  • S Suk nta

    I know increment and decrement operator depend on compiler. actually i had tried it in dev c++.

    OriginalGriffO Offline
    OriginalGriffO Offline
    OriginalGriff
    wrote on last edited by
    #21

    If you know that something is compiler dependant, then you can't say "it is like this" - because there is a very good chance that the other person is not using the same compiler! :laugh:

    Never underestimate the power of stupid things in large numbers --- Serious Sam

    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

    S 1 Reply Last reply
    0
    • L Lost User

      These operators merely add (inc) or subtract (dec) 1 from the item in question. If the operator is used in the postfix position (i++) then the original value is returned. If the operator is used in the prefix position (--i) then the new value is returned. Thus:

      int i = 10;
      int result;
      result = i++; // result equals 10, i equals 11
      result = ++i; // result equals 12, i equals 12
      result = i--; // result equals 12, i equals 11
      result = --i; // result equals 10, i equals 10

      // note do not use expressions such as
      result = ++i + i++; // results undefined

      Veni, vidi, abiit domum

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

      Am I the only one to spot that at least one of the problems in your last line is not related to increment and decrement at all? The operator+ which is invoked here needs to evaluate both of its operands, but it's undefined whether the first or second operand is evaluated first - the compiler may choose to do either! So, if the compiler evaluates the first operand first, the following happens:

      i = 10;
      i = i + 1; // pre-increment of the first argument
      first_arg = i;
      second_arg = i;
      i = i + 1; // post-increment of the second argument
      result = first_arg + second_arg; // 11 + 11

      Now consider the same on the assumption the compiler evaluates the second operand first:

      i = 10;
      second_arg = i;
      i = i + 1; // post-increment of second argument
      i = i + 1; // pre-increment of first argument
      first_arg = 1;
      result = first_arg + second_arg; // 12 + 10

      In this case, the results are the same, but that is just by coincidence - if you had used subtraction in stead of addition you'd be in for a surprise! You may want to check http://en.cppreference.com/w/cpp/language/eval_order[^] for further information regarding both function argument evaluation and increment/decrement.

      GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

      L 1 Reply Last reply
      0
      • S Stefan_Lang

        Am I the only one to spot that at least one of the problems in your last line is not related to increment and decrement at all? The operator+ which is invoked here needs to evaluate both of its operands, but it's undefined whether the first or second operand is evaluated first - the compiler may choose to do either! So, if the compiler evaluates the first operand first, the following happens:

        i = 10;
        i = i + 1; // pre-increment of the first argument
        first_arg = i;
        second_arg = i;
        i = i + 1; // post-increment of the second argument
        result = first_arg + second_arg; // 11 + 11

        Now consider the same on the assumption the compiler evaluates the second operand first:

        i = 10;
        second_arg = i;
        i = i + 1; // post-increment of second argument
        i = i + 1; // pre-increment of first argument
        first_arg = 1;
        result = first_arg + second_arg; // 12 + 10

        In this case, the results are the same, but that is just by coincidence - if you had used subtraction in stead of addition you'd be in for a surprise! You may want to check http://en.cppreference.com/w/cpp/language/eval_order[^] for further information regarding both function argument evaluation and increment/decrement.

        GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

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

        That was exactly my point.

        Veni, vidi, abiit domum

        1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          If you know that something is compiler dependant, then you can't say "it is like this" - because there is a very good chance that the other person is not using the same compiler! :laugh:

          Never underestimate the power of stupid things in large numbers --- Serious Sam

          S Offline
          S Offline
          Suk nta
          wrote on last edited by
          #24

          Thanks

          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