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

4

u/fllthdcrb Oct 15 '26 edited Oct 20 '26

Presumably, the title should have been, "Why are assignment expressions not lvalues?". But anyway...

but that doesn't really invalidate the fact that a = b resolves to an address in the program?

Except it doesn't. It resolves to the value that was assigned. That's why c = (a = b) works. The assignment on the right assigns the value of b to a, and evaluates to that value. That value then gets assigned to c. If you try to go the other way with (a = b) = c, then you're trying to assign one value to another, which is nonsensical.

EDIT: In case the terminology here is confusing, let's try a concrete example:

int a = 1, b = 2, c = 3;
(a = b) = c;

The second line first does a = b, so now both a and b are equal to 2, and that assignment evaluates to 2 as well. The expression is now equivalent to 2 = 3.