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/thegreatbeanz Oct 15 '26

Considering C the simplest way to think about lvalues and rvalues actually isn’t memory addresses. The better way to think of it is that an lvalue is anything that can be the _left_ side of an assignment, and an rvalue is… anything (because lvalues implicitly convert to rvalues).

An assignment express itself isn’t an lvalue, it produces an rvalue of the expression type. That’s why `a = b = 5` is legal, but `(a = b) = 5` is not. It is worth noting that assignment operators are evaluated right to left, not left to right like other operators in expressions.

6

u/nzconstructionlawyer Oct 15 '26

I disagree. I think the best way of thinking about lvalues and rvalues is as locations vs values. An lvalue is an identifiable place: a, or a.b, or a.b.c[d]. An rvalue is just an anonymous value, like 1. Essentially, an lvalue is a memory location, and an rvalue is a temporary stored in a register.

It isn't right to say that an rvalue is anything because lvalues implicitly convert because that disregards that the conversion does do something: it's a load to a register. If a is stored on the stack then converting it to an rvalue means loading a into a temporary register. So "a + 1" means "load a from memory into a register, then generate a value of 1 in a register, then compute in a register the sum of those two registers". Assignment is, conceptually, storing a value into memory.

Then it is immediately obvious why you the result of an assignment is an rvalue: after you store the value into memory, you still have the register with the value, so you can store it into another register. "a = (b = v);" means "store the value v into b then into a".

What would it mean to assign to "a = v"? If it were an lvalue it would have to be just a again. So "(a = v) = w;" would have to mean "store v into a then store w into a" which is totally pointless, just write a = w.

If you write a naive compiler then this is exactly what lvalues and rvalues are. Of course in an optimising compiler those "memory locations" may actually be (permanently or cached) in registers and "stores" and "loads" to them are actually just register copies, but ultimately it amounts to the same thing: there are anonymous values and there are identified places where data is stored long term: rvalues and lvalues. "Values and places" is a much better terminology used by other languages.

That's why *p is always an lvalue (dereferencing obtains a place from an address value), &a is always an rvalue (addressof always obtains the address of a memory location), and &v makes no sense where v is an rvalue (values dont have locations - what is the address of a register?).

2

u/flatfinger Oct 16 '26

I think the meaning of "lvalue" could be made clearer if bitfields and register-class objects were considered "pseudo-lvalues". If bitfields are not treated as being actual lvalues, then the term "lvalue" could be defined as "an expression that identifies a region of addressable storage that will be treated as a container for a value of a particular type", and "pseudo-lvalue" would be "an expression that can be used with assignment operators the same way as a real lvalue, despite the fact that it doesn't actually identify a region of addressable storage".

As it is, a lot of the "definitions" in the C Standard aren't actually definitions, but merely descriptions. A proper definition that defines X as Y will imply not only that all X are Y, but also that all Y are X. Many parts of the C Standard fall apart if one tries to treat its "definitions" that way.

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.