lvalue required as increment operand error

Wei picture Wei · Jun 3, 2011 · Viewed 8.4k times · Source
#include <stdio.h>

int main()
{
   int i = 10;
   printf("%d\n", ++(-i)); // <-- Error Here
}

What is wrong with ++(-i)? Please clarify.

Answer

Prasoon Saurav picture Prasoon Saurav · Jun 3, 2011

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