r/C_Programming • • Oct 15 '26

Question Why are assignment expressions lvalues?

I was reading up about lvalues and understand them now as something that resolves to an address(memory location) in the program. So a variable x is an lvalue (because it is mapped to some address on the stack) and similarly *x , x[i], strings are all lvalues. But if this is the case why is an expression a = b , not an lvalue? Here the expression would assign b to a and then return a as evident in assignments like so: int c = a = b. (I know that the equals sign is right associative and hence this would get resolved as c = (a = b), but that doesn't really invalidate the fact that a = b resolves to an address in the program?)

11 Upvotes

35 comments sorted by

View all comments

2

u/Daveinatx Oct 15 '26

In C, a is an lvalue. However, the output result of a = b is not a, rather it will output the rvalue of b. Try it out: printf ("%d\n", a=b);

1

u/choosen_one007 Oct 15 '26

I am not sure about this. Wont the assignment happen before the call to printf in this case, and so the printed value can be either a's or b's?

2

u/Sacaldur Oct 15 '26

As per definition, it will be the value of a after the assignment. This can make an observable difference in the case of a being e.g. a hardware register (writes might have no effect, or certain bits are unassignable, or a write causes another value to be read afterwards, or...).