Search code examples
coperatorslvalue

Confused with pre and post increment operator


hello I am learning basics of C programming language, recently i have studied about post and pre increment/decrement operators and also about lvalue and rvalue, the following program shows an error, lvalue required, according to me it should give a value of 6, Can anyone please explain why?

int main(){
  int x = 8, y;
  y = --x--;
  printf("y=%d",y);
  return 0;
}

Please explain, why is it so?


Solution

  • Well, let's see what is happening in --x--. At first, post-decrement executes: --(x--). (x--) = 7. After that result of this operation is placed to the original structure: --7 - doesn't make sense - thus you get lvalue required error