# Re: r- r-value vs l-value?

• From: Keith Thompson <kst-u@xxxxxxx>
• Date: Wed, 07 Jun 2006 01:35:29 GMT

"SuperKoko" <tabkannaz@xxxxxxxx> writes:
Zach wrote:
Chris Smith wrote:

Sure. An l-value is an expression that may legally appear on the left-
hand side of the assignment operator, =. An r-value is an expression
that may appear on the right-hand side. The set of r-values is a subset
of the the set of l-values. For example, given:

int a;
int b[50];

The following are l-values (and also r-values):

a
b[10]

Whereas the following are ONLY r-values:

17
a + 5
b

Therefore, the following line of code will fail to compile:

a + 5 = 15;

Hope that helps,

Where are the rules of what may go on each side?

Since there is a lot of confusion in that discussion, I'll give rules
(based on C++).

How are rules based on C++ helpful in comp.lang.c?

First, I'll correct statements of Chris Smith:
int a;
int b[50];
a is an lvalue
b[10] is an lvalue
17 is an rvalue
a+5 is an rvalue
b *is an lvalue*

b is the name of an array object. As an expression of array type,
if it's not the operand of a unary "&" or "sizeof" operator, it is
implicitly converted to a pointer. C99 6.3.2.1p3:

Except when it is the operand of the sizeof operator or the unary
& operator, or is a string literal used to initialize an array, an
expression that has type "array of _type_" is converted to an
expression with type "pointer to _type_" that points to the
initial element of the array object and is not an lvalue.

Before this conversion takes place, b is an lvalue; after the
conversion, it isn't. Since there isn't really a "before" and
"after", I suppose you could think of it as a kind of Schroedinger's
lvalue. 8-)}

[...]

lvalues are not rvalues and rvalues are not lvalues... They are totally
different concepts.

The C standard doesn't use the term "rvalue" except in a single
footnote, which says:

What is sometimes called "rvalue" is in this International
Standard described as the "value of an expression".

[...]
If an lvalue-expression is found in a context where an rvalue is
expected, an rvalue-to-lvalue conversion occurs (and this conversion is
not conceptual... Physical things occur... And HERE can happen
segmentation faults).

I think you mean an lvalue-to-rvalue conversion. I suppose this
refers to fetching the value of the designated object.

[...]

Comma operator: It doesn't expect any particular type of value (i.e. it
accepts both lvalues and rvalues with different semantics).
[...]
In C, AFAIK, the left operand can be either an rvalue or an lvalue and
the value is discarded (as in C++), but the right operand is expected
to be an rvalue, and the expression yields an rvalue.

In C, the operands are expressions. The left operand is evaluated and
its result is discarded. The right operand is then evaluated and its
result becomes the result of the operation. No need to talk about
"rvalues".

[...]

If you want to know all the C++ rules for conditional expressions, look
at:
http://www.open-std.org/jtc1/sc22/wg21/docs/wp/html/nov97-2/expr.html#expr.cond

And if you want to discuss those rules, I recommend comp.lang.c++.

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.

## Relevant Pages

• Re: r- r-value vs l-value?
... bis an lvalue ... a+5 is an rvalue ... an rvalue-to-lvalue conversion occurs (and this conversion is ... It expects an rvalue operand and yields an lvalue which designates the ...
(comp.lang.c)
• Re: Managed C++ Code Generation Bug?
... >> binds directly without an initial conversion to an rvalue, ... My mom always told me to stay out of lvalue vs. rvalue ... >I don't see why treating a static const member as an lvalue should cause ...
(microsoft.public.dotnet.languages.vc)
• Re: string literal is an lvalue; other literals are rvalues.
... >> As I understand the distinction between rvalue and lvalue, ... but not of an rvalue. ... such as the int value 5. ... There is talk of lvalue to rvalue conversion in 4.1. ...
(comp.lang.cpp)
• Re: string literal is an lvalue; other literals are rvalues.
... An lvalue denotes a storage location, an rvalue denotes a value sans location, ... There is talk of lvalue to rvalue conversion in 4.1. ... actual arguments if string literals were considered rvalues. ...
(comp.lang.cpp)
• Re: value of an assignment expression, revisited
... rvalue and lvalue seems bogus anyway. ... The WG21 will perhaps add a "weak" sequence point with assignment ... I know well what's an lvalue (and a rvalue, ... by the authors of the C++ standard, ...
(comp.std.c)