U
U
unit_9112020-10-24 14:56:54
C++ / C#
unit_911, 2020-10-24 14:56:54

Why does (++i)++ work but ++(i++) doesn't?

int k, i = 1;
k = ++(i++);


The compiler reports an error:
error: lvalue required as increment operand
What does not happen in the case of (++i)++. What is the reason?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
1
15432, 2020-10-24
@unit_911

(i++) returns a number, a constant, to put it simply. Which cannot be incremented because it is not a variable
(++i) returns a reference to a variable that can be incremented again.
for example, ++(++i) - you can. (i++)++ - you can't.

A
Adamos, 2020-10-24
@Adamos

++i - increment a variable and return it. The result of this expression is the variable itself, you can apply any other operations on it that are valid for the variable. For example, postfix increment.
i++ - return the value of a variable and then increment it. The result of this expression is not a variable, but its past value. You cannot perform operations with it that require a variable. For example, prefix increment.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question