6

Many of the times my compiler shows errors like Lvalue or Rvalue Required.

What are the Lvalues and Rvalues in C or any programming languages?

Is a pointer constant a rvalue?

2
  • Regarding "any programming languages", this differs a lot. Take Python for example, where you can do stuff like a, b = getTuple().
    – Christian
    Commented Jul 5, 2012 at 9:36
  • question seems more appropriate for SO
    – M3taSpl0it
    Commented Jul 5, 2012 at 9:44

1 Answer 1

8

The terms are somewhat language-specific; they were first introduced in CPL. Being an lvalue or an rvalue is a property of an expression; that is, every expression is either an lvalue or an rvalue.

You will often find explanations that deal with the left and right side of an assignment. That is the historical origin of the letters l and r in lvalue and rvalue, but it does not hold anymore. For example, there are lvalues that can not stand on the left side of an assignment (expressions denoting arrays and constant variables come to mind, but also class types where the assignment operator has been hidden or deleted), and at least in C++, there are rvalues that can stand on the left side of an assignment.

The best definition that I know of is: an lvalue is an expression to which the unary & operator can be applied, whereas an rvalue is an expression to which the unary & operator can not be applied. (This is not entirely true in C++ if you overload said operator, but that's probably very bad practice.)

Since C++11, the language also has the notions of xvalues, prvales und glvalues (see here and here).

Is pointer constant is a rvalue?

Expressions that denote pointer variables like p are lvalues. Expressions that denote pointer values like &i are rvalues. Both of them can be either const or non-const; that's completely orthogonal.

Java doesn't have the notion of lvalues, but in §15.1 the Language Specification states:

When an expression in a program is evaluated (executed), the result denotes one of three things:

  1. A variable (§4.12) (in C, this [expression] would be called an lvalue)
  2. A value (§4.2, §4.3)
  3. Nothing (the expression is said to be void)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.