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

Show parent comment

1

u/strange-the-quark Oct 15 '26

These names and concepts come originally from Christopher Strachey, and IMO his way of thinking about these was much better from the perspective of the programmer (language user) than the way both C and C++ standards talk about them today (although their way might be better for compiler writers).

In Strachey's original conceptualization, any expression is potentially (but not necessarily) associated with two values - an lvalue, being an object locator/address, and an rvalue being the value of interest stored there. So an expression doesn't have a value category, instead, it can potentially evaluate to one of its two associated values, and which one you get is contextual. In some situations, you want the rvalue, in other situations you want the lvalue, and if what you want cannot be inferred, then you can use some special syntax to specify (or the language can require you to always be explicit, or whatever).

IMO from the programmer's perspective, it doesn't need to be any more complicated than that, and the error messages and warnings would be much better and much more useful at that level of abstraction; meanwhile, the compiler can do whatever it wants under the hood, it can treat expressions as having a fixed value category, and can do implicit lvalue-to-rvalue conversions and whatnot.