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

15

u/EpochVanquisher Oct 15 '26

If an assignment were an lvalue, you would be able to do things like this:

(x = y) = z; // Assign twice???? Dead store!
ptr = &(x = y); // Wut?

These aren’t useful. Because they’re not useful, assigments aren't lvalues.

Note that assigment chaining is a little more useful:

// Both of these are the same.
x = y = z;
x = (y = z);

Also note that it's different in C++, because people in C++ do dumb stuff like this:

std::cout << "hello, " << "world!\n";

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.

2

u/choosen_one007 Oct 15 '26

Yeah this makes a lot of sense. I came up with another example like so:

int *get_some_pointer();


int main() {
  int *local_some_pointer = get_some_pointer() = NULL;

  return 0;
}

int *some_pointer = NULL;

int *get_some_pointer(){
  return some_pointer;
}

In this case the int *local_some_pointer = get_some_pointer() = NULL; highlights the case where an assignment can never resolve to an lvalue, since get_some_pointer() is not an lvalue. And so for all cases a=b, cannot be resolved as an lvalue

0

u/EpochVanquisher Oct 15 '26

Correct, although if we're gonna follow existing C syntax, you’d have to split it:

  int *local_some_pointer;
  (some_local_pointer = get_some_pointer()) = NULL;