Are literal strings and function return values lvalues or rvalues?

Tim picture Tim · Jan 10, 2010 · Viewed 12.4k times · Source
  1. Just wonder if a literal string is an lvalue or an rvalue. Are other literals (like int, float, char etc) lvalue or rvalue?

  2. Is the return value of a function an lvalue or rvalue?

How do you tell the difference?

Answer

Roger Pate picture Roger Pate · Jan 10, 2010
  1. string literals are lvalues, but you can't change them
  2. rvalue, but if it's a pointer and non-NULL, the object it points to is an lvalue

The C standard recognizes the original terms stood for left and right as in L = R; however, it says to think of lvalue as locator value, which roughly means you can get the address of an object and therefore that object has a location. (See 6.3.2.1 in C99.)

By the same token, the standard has abandoned the term rvalue, and just uses "the value of an expression", which is practically everything, including literals such as ints, chars, floats, etc. Additionally, anything you can do with an rvalue can be done with an lvalue too, so you can think of all lvalues as being rvalues.