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

2

u/thegreatbeanz Oct 15 '26 edited Oct 15 '26

To quote the C89 spec sub clause 3.2.2.1:

A difference of opinion within the C community has centered around the meaning
of lvalue, one group considering an lvalue to be any kind of object locator, another
group holding that an lvalue is meaningful on the left side of an assigning operator.
The Committee has adopted the definition of lvalue as an object locator. The term
modifiable lvalue is used for the second of the above concepts.

Happy to see that even after 37 years we can still disagree on this point ;P

Edit: I guess it is also worth adding the C99 footnote 53:

The name ‘‘lvalue’’ comes originally from the assignment expression E1 = E2, in which the left
operand E1 is required to be a (modifiable) lvalue. It is perhaps better considered as representing an
object ‘‘locator value’’. What is sometimes called ‘‘rvalue’’ is in this International Standard described
as the ‘‘value of an expression’’.

4

u/nzconstructionlawyer Oct 15 '26

Well yeah there's also that: you can have const lvalues which are nonsensical on the left side of an assignment operator.

But more fundamentally I just think the assignment operator definition is too extensional. It is "an lvalue is something you can assign to" which doesn't tell someone what an lvalue actually is. It's like saying a pointer is something you can dereference. Like, yeah, but why can you and what does that mean? What does that abstract definition capture?

I prefer the concrete and intensional explanation even if you have to caveat it with "up to optimisation". Because it helps people to understand why we have these concepts to begin with, and that is: if you are writing a compiler, how do you implement complex expressions like "*q = b + \*(p + 10);". And of course the answer is: classify things as memory or register and when you need to treat memory as a register implicitly insert a load.

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.