Lvalue required as an increment operand error

#include <stdio.h> int main() { int i = 10; printf("%d\n", ++(-i)); // <-- Error Here } 

What happened to ++(-i) ? Please clarify.

+6
source share
4 answers

-i generates a temporary value, and you cannot apply ++ to the temporary (generated as a result of the rvalue expression). Pre increment ++ requires its operand to be lvalue, -i not lvalue, so you get an error.

+7
source

The ++ operator increments the value of a variable. (Or, more precisely, lvalue is something that may appear on the l eft side of an expression)

(-i) not a variable, so there is no point in increasing it.

+5
source

You cannot increment a temporary one that does not have an identifier . You need to save this in something to increase it. You can think of the l-value as something that can appear on the left side of the expression, but in the end you will need to think about it in terms of something that has a personality but cannot be moved (C terminology ++ 0x). Bearing in mind that it has an identifier, the link refers to the object, what you would like to save.

(- i) has no identity, therefore nothing is said there. Without referring to it with anything, there is no way to save something in it. You cannot reference (-i), so you cannot increase it.

try i = -i + 1

 #include <stdio.h> int main() { int i = 10; printf("%d\n", -i + 1); // <-- No Error Here } 
+1
source

Try this instead:

 #include <stdio.h> int main() { int i = 10; printf("%d\n", (++i) * -1); } 
0
source

Source: https://habr.com/ru/post/889720/


All Articles