-1

Following is line of code in c.

     i++=i++;

Output:Error Lvalue required.
So what does this error indicate?

ghostrider
  • 2,046
  • 3
  • 23
  • 46
  • 2
    That C is different from C++. `i++` returns an integer value, not a reference to `i`. So you can't assign it. – user703016 Aug 05 '12 at 11:48
  • 1
    Possible duplicate of http://stackoverflow.com/questions/1353384/l-value-required-error . – DCoder Aug 05 '12 at 11:48
  • @Cicada So evaluation begins from right ie i is assigned to left of "=".But since left of = doesnot have any address therefore it reports error.So then what is rvalue in this code? – ghostrider Aug 05 '12 at 11:51
  • Whatever is on the right hand side of = – Tony Hopkinson Aug 05 '12 at 11:55
  • @Tony Hopkinson ok so lvalue is an address and rvalue is the constant or value of any variable definrd earlier? – ghostrider Aug 05 '12 at 11:59
  • Logical equivalent would actually be `i=i; i+=2;`, which more simply is just `i+=2;`. Why not just use that? – Bingo Aug 05 '12 at 12:35

3 Answers3

1

The result of i++ is not a lvalue and the standard requires the left operand of the assignment operator to be a lvalue.

Even if it was allowed your program would invoke undefined behavior as you are trying to modify the same object twice between two sequence points.

ouah
  • 142,963
  • 15
  • 272
  • 331
  • can you please explain what is a sequence point in simple terms? – ghostrider Aug 05 '12 at 11:54
  • @ghostrider a sequence point is a certain point in the program execution where the values are synchronized. The C standard details all the sequence points. For example, in `i++=i++;` there is a sequence point before the whole expression and after the `;`. – ouah Aug 05 '12 at 11:58
  • Can you please give a more simpler explaination? – ghostrider Aug 05 '12 at 12:01
0

It indicates that i++ is not assignable. It is like trying to assign to 1 (which i++ will yield when i stored the value 1).

Of course that makes no sense. You can compare it to trying to change the color "red", when you actually wanted to first paint your paper red and then blue.

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
0

It's saying left value required. You are doing an assignment left = right. i++ can't be a left because it's a right. Remove the terseness and your line of code sort of becomes

i + 1 = i + 1, which is an equation not an assignment.

On top of that what are you actually trying to do?

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39