Menu

++a or a++

Riaan
2008-03-30
2012-09-26
  • Riaan

    Riaan - 2008-03-30

    What does ++a or a++ mean?

    And --a or a--?

     
    • cpns

      cpns - 2008-03-30

      Get yourself a good C++ reference, these are fundamental C/C++ operators. If you have not come across then you probably do not have enough reference material to really understand much of the language at all.

      At the risk of pointlessly repeating material you can find in any C/C++ reference and all over the internet, here's a brief explanation. However try to avoid asking on a forum what you could easily look up; it is a slow and painful way of learning, and you have no way of knowing whether what you are being told is accurate.

      ++ is the increment operator, both a++ and ++a on their own are equivalent to a = a + 1. ++ is not actually one operator but two; ++a is the pre-increment, and a++ is the post-increment. The difference is in the order of evaluation.

      x = ++a ;

      is equivalent to:

      a = a + 1 ;
      x = a ;

      wheras

      x = a++ ;

      is equivalent to:

      x = a ;
      a = a + 1 ;

      Now this seems fairly simple, but in more complex expressions the evaluation rules are so arcane and in some cases undefined that it is best to avoid using ++ in anything other than a simple expression involving the operator and a single variable. If you do that, for simple data types therefore it makes no difference whether you use a pre-increment or a post-increment. I habitually use the postfix form for purely aesthetic reasons, however, in C++ the operator can be overloaded for a class type, and here for reasons I won't go into, the pre-increment operator is sometimes efficient, so if you are going to get into a habit, then use the pre-increment and to hell with trivial aesthetics. I'm too old to change perhaps, and started coding in C where it really made no difference. And the language is C++ rather than ++C after all! ;-)

      -- is the decrement operator, a-- is equivalent to a = a - 1, and all the above for ++ holds for -- to.

      See also: http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.15

      Clifford

       
    • Anonymous

      Anonymous - 2008-03-31

      Clifford worte:

      > x = a++ ;

      > is equivalent to:

      > x = a ;
      > a = a + 1 ;

      So x is never incremented using that expression?

       
      • cpns

        cpns - 2008-03-31

        Was that meant to be a question or a statment? Remove the question mark, and you are correct.

        In fact x was never incremented in either example, only a was ever the operand for ++. x was merely assigned the value of a, either before or after the increment depending upon whether postfix ++ or prefix ++ was used.

        Basically when a++ appears in an expression, the expression is evaluated and then a is incremented. When ++a is used a is incremented before the expression is evaluated. Now when you start manipulating evaluation order using parentheses, it all gets a bit hairy and confusing and it is best avoided. Essentially it is an assignment, and you would not normally put an assignment as part of a larger expression, even if it is valid - because it is just too confusing. For example, most programmers would consider the following unusual:

        x = a = a + 1 ;

        so why would you write the following?:

        x = a++ ;

        Write either:

        a++ ;
        x = a ;

        or

        x = a
        a++ ;

        as you require, clearer and less error prone. Or if you prefer and for teh reasons already mentioned:

        ++a ;
        x = a ;

        or

        x = a
        ++a ;

        These are of course trivial examples, an most programmers could cope with x = a++ or x = ++a, but it is a habit best avoided lest by familiarity (or code maintenance) you attempt to add to such an expression and render it unintelligible (or just incorrect).

        Clifford

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.