Win a copy of Penetration Testing Basics this week in the Security forum!

# Prefix postfix increment question

Montgomery Buslingthorpe
Greenhorn
Posts: 4

This code should be very simple, but somehow I am failing to understand why it prints -2. I seem to be missing something important about postfix and/or prefix increments.
Could somebody enlighten me here please ?

Greg Brannon
Bartender
Posts: 563
Consider this:

and see if it helps.

Montgomery Buslingthorpe
Greenhorn
Posts: 4
Ahhh, breaking it up like that, explains a lot :P

That results in -1 += -1 / 1

Thanks Greg

I've read a couple of times where people were saying that var++ is only incremented after the statement but to my understanding, the statement is var += var++/++var;

Breaking it up implies to me that var++ is incremented right after the / but before the ++var, (and therefore within and not after the statement).

If breaking up the parts like you mentioned, can be applied as a general rule of thumb, then these kind of lines would make a lot more sense to me.

Campbell Ritchie
Sheriff
Posts: 50632
82
Montgomery Buslingthorpe wrote:. . . people were saying that var++ is only incremented after the statement . . .
That is an over‑simplification, and very inaccurate.
The truth of the matter is, there are two values going. The value of var which is 0 just before the / and the value of var++. The value of var++ is the same as the old value of var, i.e. -1.
The same applies to the second half of the expression. There is a value for var and a value for ++ var, but they are both the same at (+)1.
If you search these fora, you will find many similar questions; this causes confusion for everybody.

Henry Wong
author
Marshal
Posts: 21697
85
Montgomery Buslingthorpe wrote:
I've read a couple of times where people were saying that var++ is only incremented after the statement but to my understanding, the statement is var += var++/++var;

Breaking it up implies to me that var++ is incremented right after the / but before the ++var, (and therefore within and not after the statement).

If breaking up the parts like you mentioned, can be applied as a general rule of thumb, then these kind of lines would make a lot more sense to me.

Think of pre/post increments as having a side effect.... with post increment, the side effect happens right after the variable is used, and with pre increment, the side effects happens right before the variable is used.

The other thing to remember is that these side effects happen during evalution of the expression. This means that it happens based on the order of evaluation -- and not based on precedence and associativity. For your example, it makes no difference, but throw in a few more pre and post increments, and a few parens, and you can see the difference.

Henry

Paul Clapham
Sheriff
Posts: 21443
33
The most important thing to remember about the expressions a++ and ++a? Remember not to use them.

You may have to learn how they work if you're taking those certification exams, but trying to actually use them is going to be more trouble than it's worth. As you've already seen, trying to figure out the value of an expression which includes a++ or ++a in it is not easy. And you shouldn't write code which is not easy to understand.