Skip to main content

How should postincrement work?

Posted by rdtindsm on July 29, 2009 at 5:09 PM PDT

Let me acknowledge that thhis is more or less a cross post from another thread.

The original poster asked why the expression
i=i++; evaluates to 10 rather than 11.

In the discussion, the answer is that the assignment is made before i is incremented. I to would expect the expression to evaluate as 11.

referring to
15.14.2 Postfix Increment Operator ++

PostfixExpression ++

A postfix expression followed by a ++ operator is a postfix increment expression. The result of the postfix expression must be a variable of a type that is convertible (§5.1.8) to a numeric type, or a compile-time error occurs. The type of the postfix increment expression is the type of the variable. The result of the postfix increment expression is not a variable, but a value.

At run time, if evaluation of the operand expression completes abruptly, then the postfix increment expression completes abruptly for the same reason and no incrementation occurs. Otherwise, the value 1 is added to the value of the variable and the sum is stored back into the variable. Before the addition, binary numeric promotion (§5.6.2) is performed on the value 1 and the value of the variable. If necessary, the sum is narrowed by a narrowing primitive conversion (§5.1.3) and/or subjected to boxing conversion (§5.1.7) to the type of the variable before it is stored. The value of the postfix increment expression is the value of the variable before the new value is stored.

Note that the binary numeric promotion mentioned above may include unboxing conversion (§5.1.8) and value set conversion (§5.1.13). If necessary, value set conversion is applied to the sum prior to its being stored in the variable.

A variable that is declared final cannot be incremented (unless it is a definitely unassigned (§16) blank final variable (§4.12.4)), because when an access of such a final variable is used as an expression, the result is a value, not a variable. Thus, it cannot be used as the operand of a postfix increment operator.

My feeling is that the expression
should assign a value of 10 to i. The reference is clear that a value for i is created and incremented. It also says that that this value is subsequently assigned to i unless "the operand expression completes abruptly". Perhaps this is the key to the quandry, but I have now idea what "completes abruptly" means, or how it affects the evaluation.

Adding to the problem is that
i=i++; evaluates to 10 in Java and 11 in C/C++;

Likewise, i=i + i++ evaluates to 20 in Java and 21 in C/++.

This bothers me immensly. I should think the evaluation would be the same, given the similarity between the two languages. It certainly makes porting applications more difficult.

It would seem to me that the expression i++ would increment i at some point. There is of course some ambiguity as to what happens in the expression i + i++. You can't assign the incremented value of i to i after evaluating and assigning i. That would clearly leave i in an incorrect state.

I can see that the expression i = i + i++ would initialy assign the value of 20 to i. The problem comes as to whether i should subsequntly be incremented to 21, as C evidently does.

Am looking for a clearer explanation of what is going on.


Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Re: How should postincrement work?

Post increment increments the value of the variable by one after the execution to the statement. It means that if we write “i++” then the value of the “I” will increment after its execution.
Data Recovery