-4

Can somebody explain the compilation error in the program

#include<stdio.h>
int main()
{
   int i = 10;
   printf("%d", ++(-i));
   return 0;
}
Deduplicator
  • 44,692
  • 7
  • 66
  • 118

1 Answers1

1

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).

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278