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?
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:
- A variable (§4.12) (in C, this [expression] would be called an lvalue)
- A value (§4.2, §4.3)
- Nothing (the expression is said to be void)
a, b = getTuple()
.