r/C_Programming • u/choosen_one007 • 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?)
19
u/MyNameIsHaines Oct 15 '26
Assigning something to (a = b) would be nonsensical no?
15
u/EpochVanquisher Oct 15 '26
If an assignment were an lvalue, you would be able to do things like this:
These aren’t useful. Because they’re not useful, assigments aren't lvalues.
Note that assigment chaining is a little more useful:
Also note that it's different in C++, because people in C++ do dumb stuff like this:
This kind of dumb shit somehow made it into the language. It’s easily one of the worst parts of C++. Thankfully, it’s obsolete, and you can just call
std::print()now.6
u/SmokeMuch7356 Oct 15 '26
a = bdoes not resolve to an address; it evaluates to whatever gets stored ina(after any type conversions).a = bis not an lvalue because the language definition says so:But let's assume for the moment that
a = bis an lvalue; this would mean thatis legal, but in that case what should be the result? Where does the value of
cget stored? Ina? Inb? What happens to the value ofb?2
u/Daveinatx Oct 15 '26
In C,
ais an lvalue. However, the output result ofa = bis nota, rather it will output the rvalue ofb. Try it out:printf ("%d\n", a=b);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.
4
u/fllthdcrb Oct 15 '26 edited Oct 20 '26
Presumably, the title should have been, "Why are assignment expressions not lvalues?". But anyway...
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 ofbtoa, and evaluates to that value. That value then gets assigned toc. 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:
The second line first does
a = b, so now bothaandbare equal to 2, and that assignment evaluates to 2 as well. The expression is now equivalent to2 = 3.3
u/Certain-Flow-0 Oct 15 '26
An L-value is anything that can appear to the left of a = and must follow this parenthesization: (l-value) = (expression). (a = b) = c doesnt compile, that means a = b isn’t an l-value
2
u/zhivago Oct 15 '26
Consider given (a = b) = c; if (a = b) were an lvalue then this would end up simultaneously assigning two values to a which would have undefined behavior.
So even if this were a sensible thing to do it would break under C's sequencing model.
1
u/spellstrike Oct 15 '26
might make sense to put some very simple programs into https://godbolt.org/ to understand what the assembly output is generated to explore this on your own.
1
u/JGhostThing Oct 15 '26
The simple explanation is that an lvalue can appear on the LEFT side of an assignment statement. A rvalue can only appear on the right side.
So, x is an lvalue.
x + 3 is an rvalue.
x = x +3;
In other words, you can assign to an lvalue, and assign from an rvalue.
1
u/GhostVlvin Oct 15 '26
a = b evaluates to address of a, perhaps to avoid copying
1
u/ttv90 Oct 15 '26
The spec wording is always the hard part.
1
u/Sacaldur Oct 15 '26
One small nitpick: an lvalue doesn't necessarily resolve to an address. A local variable might not be put onto the stack at all, but rather only into a CPU register, and CPU registers don't have memory addresses.
1
u/thisisignitedoreo Oct 16 '26
any lvalue can be an rvalue (any address can be dereferenced to get the value), but not vice versa. a = b works, a = 2 works but 2 = a doesnt