Are literal strings and function return values lvalues or rvalues?
Asked Answered
T

3

31
  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?

Thumbnail answered 10/1, 2010 at 20:8 Comment(4)
I am not sure what do you mean with "lvalue" and "rvalue". Do you mean that this value can stand on left or right side from operator? If yes, you should specific your question, because you can write to all non const variables. return value of function is const.Crellen
@Gaim: lvalue and rvalue are common terms with specific meanings.Resource
@Gaim: lvalue and rvalue are compiler and languag specification level speak. In this case they refer to the syntax of C.Eunuchize
A good article is here eli.thegreenplace.net/2011/12/15/…Mandamandaean
C
40
  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.

Cyr answered 10/1, 2010 at 20:11 Comment(10)
+1 for short and correct answer. I had no idea the C standard does not use "rvalue" anywhere, but actually that's the case (just did a search!). Another interesting difference to C++. Another that i crossed over some day is that function designators are rvalues in C, while they are lvalues in C++. So op& in C can be applied to some rvalues - those that designate functions.Clemen
It does use rvalue in the index. :PCyr
@Roger: Thanks! I just wonder about other literals other than string literal? @Johannes: I just saw function desinators are lvalues somewhre online yesterday, and remembered it was for C. Am I wrong?Thumbnail
Tim: I mentioned those specifically at the end, just about everything is an rvalue, and those literals are not lvalues.Cyr
C99, 6.3.2.1/4: "A function designator is an expression that has function type." I believe that is saying they are rvalues, as they do not have object type. I linked to where you can find the C standard, if you're really curious, but I make no warranty on your sanity after standardese.Cyr
@Roger: Thanks! I meant to ask if literals other than string literal are lvalue.Thumbnail
In particular, compound literals in C99 are lvalues. So you can in fact do int *p = &(int){ 42 }; printf("%d", *p);.Clemen
Hadn't thought about that before, but it makes sense in a weird way and is consistent with string literals.Cyr
Johannes: what does that {} mean in your compound literal example. I tried () instead, and it is wrong because it is still rvalue not lvalue.Thumbnail
Tim: That's just part of the compound literal syntax. If you change it to parens, then you have (int)(42) which is the same as (int)42 which is just a redundant cast, and indeed an rvalue.Cyr
P
4

There are two kinds of expressions in C: 1. lvalue: An expression that is an lvalue may appear as either the left-hand or right-hand side of an assignment. 2. rvalue: An expression that is an rvalue may appear on the right- but not left-hand side of an assignment. Variables are lvalues and so may appear on the left-hand side of an assignment. Numeric literals are rvalues and so may not be assigned and cannot appear on the left-hand side. Following is a valid statement: int g = 20; But following is not a valid statement and would generate compile-time error: 10=20;

Porfirioporgy answered 7/2, 2013 at 13:23 Comment(0)
P
1

there's a definition for C++ from Microsoft. By this definition, a literal string, say "hello world", is lvalue, because it's const and not temporary. Actually it persists across your application's lifetime.

Pinkston answered 25/5, 2011 at 5:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.