Post-increment and Pre-increment concept?

135,113

Solution 1

All four answers so far are incorrect, in that they assert a specific order of events.

Believing that "urban legend" has led many a novice (and professional) astray, to wit, the endless stream of questions about Undefined Behavior in expressions.

So.

For the built-in C++ prefix operator,

++x

increments x and produces (as the expression's result) x as an lvalue, while

x++

increments x and produces (as the expression's result) the original value of x.

In particular, for x++ there is no no time ordering implied for the increment and production of original value of x. The compiler is free to emit machine code that produces the original value of x, e.g. it might be present in some register, and that delays the increment until the end of the expression (next sequence point).

Folks who incorrectly believe the increment must come first, and they are many, often conclude from that certain expressions must have well defined effect, when they actually have Undefined Behavior.

Solution 2

int i, x;

i = 2;
x = ++i;
// now i = 3, x = 3

i = 2;
x = i++; 
// now i = 3, x = 2

'Post' means after - that is, the increment is done after the variable is read. 'Pre' means before - so the variable value is incremented first, then used in the expression.

Solution 3

The difference between the postfix increment, x++, and the prefix increment, ++x, is precisely in how the two operators evaluate their operands. The postfix increment conceptually copies the operand in memory, increments the original operand and finally yields the value of the copy. I think this is best illustrated by implementing the operator in code:

int operator ++ (int& n)  // postfix increment
{
    int tmp = n;
    n = n + 1;
    return tmp;
}

The above code will not compile because you can't re-define operators for primitive types. The compiler also can't tell here we're defining a postfix operator rather than prefix, but let's pretend this is correct and valid C++. You can see that the postfix operator indeed acts on its operand, but it returns the old value prior to the increment, so the result of the expression x++ is the value prior to the increment. x, however, is incremented.

The prefix increment increments its operand as well, but it yields the value of the operand after the increment:

int& operator ++ (int& n)
{
    n = n + 1;
    return n;
}

This means that the expression ++x evaluates to the value of x after the increment.

It's easy to think that the expression ++x is therefore equivalent to the assignmnet (x=x+1). This is not precisely so, however, because an increment is an operation that can mean different things in different contexts. In the case of a simple primitive integer, indeed ++x is substitutable for (x=x+1). But in the case of a class-type, such as an iterator of a linked list, a prefix increment of the iterator most definitely does not mean "adding one to the object".

Solution 4

No one has answered the question: Why is this concept confusing?

As an undergrad Computer Science major it took me awhile to understand this because of the way I read the code.

The following is not correct!


x = y++

X is equal to y post increment. Which would logically seem to mean X is equal to the value of Y after the increment operation is done. Post meaning after.

or

x = ++y
X is equal to y pre-increment. Which would logically seem to mean X is equal to the value of Y before the increment operation is done. Pre meaning before.


The way it works is actually the opposite. This concept is confusing because the language is misleading. In this case we cannot use the words to define the behavior.
x=++y is actually read as X is equal to the value of Y after the increment.
x=y++ is actually read as X is equal to the value of Y before the increment.

The words pre and post are backwards with respect to semantics of English. They only mean where the ++ is in relation Y. Nothing more.

Personally, if I had the choice I would switch the meanings of ++y and y++. This is just an example of a idiom that I had to learn.

If there is a method to this madness I'd like to know in simple terms.

Thanks for reading.

Solution 5

It's pretty simple. Both will increment the value of a variable. The following two lines are equal:

x++;
++x;

The difference is if you are using the value of a variable being incremented:

x = y++;
x = ++y;

Here, both lines increment the value of y by one. However, the first one assigns the value of y before the increment to x, and the second one assigns the value of y after the increment to x.

So there's only a difference when the increment is also being used as an expression. The post-increment increments after returning the value. The pre-increment increments before.

Share:
135,113
Saad Masood
Author by

Saad Masood

Full Stack Developer

Updated on July 05, 2022

Comments

  • Saad Masood
    Saad Masood almost 2 years

    I don't understand the concept of postfix and prefix increment or decrement. Can anyone give a better explanation?

  • Dean Burge
    Dean Burge over 13 years
    The two lines are not equal at all. -1
  • Dean Burge
    Dean Burge over 13 years
    "the increment is done after the variable is read". I used to think I understand post- and prefix increment, but now you got me confused!
  • Jonathan Wood
    Jonathan Wood over 13 years
    wilhemtell: Thanks for adding absolutely nothing to the discussion. Good job.
  • Dean Burge
    Dean Burge over 13 years
    @JonathanWood what's the type of x? There you have it. The two lines are not equal. Do I add anything to the discussion now?
  • James McNellis
    James McNellis over 13 years
    Posting a large block of text from the ISO Standard without any comment or explanation is not really helpful, especially when the text is not entirely self-contained (from this text, what is a sequence point? what is an lvalue? how can this lvalue be qualified or unqualified? where are "the discussions of additive operators and compound assignment"?). Also, assuming that C++ is the same is generally A Bad Idea. There are many small but important differences between the two languages, even in supposedly simple things like operator behavior.
  • Dean Burge
    Dean Burge over 13 years
    @JamesMcNellis no, the first two statements have the same effect unless x is of a class-type period.
  • Cheers and hth. - Alf
    Cheers and hth. - Alf over 13 years
    @Sa'me Smd: "lvalue" is essentially a reference to an object that has storage. it's terminology stemming from early C, where an assignment needed an lvalue on the left side.
  • Cheers and hth. - Alf
    Cheers and hth. - Alf over 13 years
    Hm, I goofed in the final paragraph. I'll edit this later. It's complex, so so some standard-quoting and concrete examples also needed. Sorry...
  • Dean Burge
    Dean Burge over 13 years
    The first is a prefix increment, not postfix increment.
  • Cheers and hth. - Alf
    Cheers and hth. - Alf over 13 years
    @wilhemtell: thanks. I must have been writing that with head switched off! Also final para is silly. Perhaps also next to final para. I'll fix that typo straight away, but need more coffee first for the rest! :-)
  • James McNellis
    James McNellis over 13 years
    @wilhelmtell: Ok, right. Even with consistent operator overloads, you still end up copying the object when you postincrement.
  • Dean Burge
    Dean Burge over 13 years
    @JamesMcNellis you will end up calling two different functions, to begin with.
  • James McNellis
    James McNellis over 13 years
    @wilhelmtell: If the overloads are consistent (which they should be), then the effect of the statements x++; and ++x; is the same. Yes, you call different functions, but they should do the same thing. That's what I was trying to get at.
  • sje397
    sje397 over 13 years
    You're right about the mechanics but I think you're wrong to say others and incorrect on the conceptual level. The mechanics are just practicalities of trying to implement the concept. 'Post' means after - conceptually, the increment is done afterwards. The whole concept of 'sequence' as in 'sequence point' implies an order, and the UB comes from misunderstanding how sequence points work, not from 'misunderstanding' the meaning of 'pre' and 'post'.
  • sje397
    sje397 over 13 years
    Why is that confusing? We're talking about the concept of 'pre' and 'post' increment. Those prefixes do mean before and after.
  • James McNellis
    James McNellis over 13 years
    The "pre" and "post" in "preincrement" and "postincrement" refer to the position of the operator with respect to the operand. They do not imply any temporal ordering between when the increment occurs with respect to when the variable is read.
  • sje397
    sje397 over 13 years
    @James: I understand your point - but to illustrate, imagine how confusing it would be if the function of the operators was reversed. I know that technically, there is no implication of temporal ordering, but there is definitely a conceptual mapping of the post increment to the idea of 'incrementing after use'.
  • Cheers and hth. - Alf
    Cheers and hth. - Alf about 12 years
    @sje397: in this context, pre and post refer to prefix and postfix operators, i.e. the order of symbols in the source code, not a time order of events. however, historically i believe that the C operators reflected similar machine code instructions.
  • AJMansfield
    AJMansfield over 11 years
    So what would an expression like (i++ > 0) && someProperty(myArray[i]) do? Like if i=5, would it be calling someProperty with myArray[5] or myArray[6]?
  • Cheers and hth. - Alf
    Cheers and hth. - Alf over 11 years
    @AJMansfield: In C++03 terms the built-in && introduces a sequence point (C++03 §1.9/18). In C++11 terms the left hand operand expression of the built-in && is sequenced before the right hand operand expression (C++11 §5.14/2). This means that if the call is made, it's made with i value 6.
  • AJMansfield
    AJMansfield over 11 years
    @Cheers and hth. - Alf one other question, what about other operators? x++ + y(x)? or the equivalent with ++x instead? If I had access to a c++ environment right now I'd probably just try it and see, but unfortunately, I don't...
  • Cheers and hth. - Alf
    Cheers and hth. - Alf over 11 years
    @AJMansfield: The && and || are unique in providing short-circuit evaluation (and hence sequence points). The ternary choice operator is a bit like that, in that it guarantees that the choice not taken is not evaluated. But for arithmetic operators you just get Undefined Behavior when you both modify and use a variable in the same expression. I think, but not sure, that this is so also for all other operators. It is anyway the best assumption, and it's not good to write code that relies on some subtle special case that few if any programmers know about. ;-)
  • Pedro J. Ponce de León
    Pedro J. Ponce de León almost 11 years
    This is due probably to what the C99 standard says: "The side effect of updating the stored value of the operand shall occur between the previous and the next sequence point."
  • daramarak
    daramarak about 10 years
    I am sorry but posting a part of the standard from a different language isn't particular helpful. Most of the time the increment operators in c++ are used on class types, which makes this more confusing than helpful.
  • R.M.VIVEK ARNI
    R.M.VIVEK ARNI over 9 years
    first increment the value is pre increment and next step increment the value is post increment
  • Graza
    Graza about 9 years
    "If there is a method to this madness I'd like to know in simple terms." - I think of it as this: PRE increment (y=++x): increment x first THEN assign to y. And for POST increment (y=x++): Assign to y THEN increment x. So think of pre & post as "when does x get incremented" rather than "what version of x does y get". That's my "method to the madness" and it makes perfect sense, IMO ;-)
  • du369
    du369 over 8 years
    And that is the same reason why "++x" is allowed to be on the left hand side of the simple assignment operator "=" while the expression "x++", which doesn't produce a "lvalue", is not. Am I correct?
  • Foo Bar
    Foo Bar about 8 years
    I tried this with C and C++ compilers. It also evaluates to 6. With what compiler did you get 3?
  • Galik
    Galik almost 8 years
    This is undefined behavior when using the pre/post increment operators twice in one expression.
  • underscore_d
    underscore_d over 7 years
    "If there is a method to this madness I'd like to know in simple terms." well, of course there is. preincrement means 'the operator comes pre a.k.a. before the operand, so the increment comes before the operand is returned to the caller, so the value they get includes the increment.' postincrement means 'the operator comes post a.k.a. after the operand, so the increment comes after (a copy of) the operand is returned to the caller, so the value they get does not include the increment.'
  • Happy Green Kid Naps
    Happy Green Kid Naps over 7 years
    The statement "in C/C++ the expression a++ + a++ + a++ evaluates to 3" is simply wrong. As @Galik noted, it results in undefined behavior.
  • Mark Amery
    Mark Amery over 7 years
    Do expressions like i++ || foo(i) really produce undefined behaviour (of the nasal demon variety) as your second paragraph seems to imply, or merely unspecified behaviour? Spec quotes backing up your claim here would help.
  • Cheers and hth. - Alf
    Cheers and hth. - Alf over 7 years
    @MarkAmery: In C++03 the normative text said undefined behavior, while the examples said unspecified behavior. So it's fair to say that the committe was a bit unclear on which it should be, and Andrew Koenig, then secretary, failed to catch that. But formally it's always been real undefined behavior, nasal demons & the full package.
  • umbreonben
    umbreonben about 7 years
    It's a C operator, not a C++ operator. C++ stole it from C.
  • Rajesh
    Rajesh almost 7 years
    @JonathanWood Thanks for the lucid answer. The distinction between usage of increment in an expression and as a standalone operation is the reason most people get confused. Your answer clearly explains this
  • Rajesh
    Rajesh almost 7 years
    Thumbs up for explaining with an example
  • Pharap
    Pharap almost 5 years
    ++y is preincrement because the ++ is used as a prefix, y++ is postincrement because ++ is used as a postfix (or 'suffix'). Not contrary to the English language at all.
  • Daniel Yankowsky
    Daniel Yankowsky about 4 years
    "The words pre and post are backwards with respect to semantics of English." I don't agree with that. "Pre" and "post" are modifying "increment", and accurately describe when the increment conceptually occurs. "Pre-increment" conceptually increments before producing a value. "Post-increment" conceptually increments after producing a value. So with pre-increment, you get the incremented value. With post-increment, you get the original value.
  • RaHuL
    RaHuL about 4 years
    @sje397 why for example int a = 5; cout << a++; prints only 5 instead of 6 according to your example?
  • Caleth
    Caleth about 4 years
    @aaa90210 It's an operator, present in both C and C++, among other languages