Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused with pre and post increment operator [duplicate]

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?

like image 254
Mayank Tiwari Avatar asked Nov 24 '25 08:11

Mayank Tiwari


1 Answers

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

like image 79
Pavel Dudka Avatar answered Nov 26 '25 16:11

Pavel Dudka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!