Search code examples
cincrementlvaluervaluepre-increment

l value required as increment operand


Can somebody explain the compilation error in the program

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

Solution

  • The result of -i is an r-value, not an l-value. You can't increment r-values with the ++ or -- operators; you can only increment an l-value.

    Roughly, an l-value could appear on the LHS (left-hand side) of an assignment. You could not write:

    -i = -i + 1;
    

    For the same reason, you cannot write ++(-i).