What is the Lvalue and Rvalue?
Answers were Sorted based on User's Feedback
Simple definitions:
Lvalue - is value which can be modified(it cannot be a
constant). And it can act as Rvalue too.
Rvalue - is value which can be able to fetch from the
memory, propably a constant. It can also act as Lvalue(if
it is not a constant).
Dont be confused... Lets hav example.
Example:
int a = 0, b = 10;
1.Rvalue
if(a)
{
...
...
...
}
here a is RValue(cos, as per definition, the value is able
to fetch from the memory)
2.One variable acting as L and R value
a = a + b;
here a + b; evaluated fist.. lets analise...
First a and b has to be fetched from the memory, by this
time, both will act as a Rvalue(as per definition)....
then a + b result shall be assigned to 'a'. Now 'a' will be
acting as a Lvalue. Cos u able to modify it...
So here 'a' can act as L as well as R value depends on the
situation.
3.a = 10;
Here 10 is Rvalue and 'a' ll act as Lvalue.
Hope u understand clearly.
| Is This Answer Correct ? | 6 Yes | 1 No |
Answer / ashish
An object is a contiguous region of memory storage. An
lvalue (pronounced: L value) is an expression that refers to
such an object. The original definition of lvalue referred
to "an object that can appear on the left-hand side of an
assignment." However, const objects are lvalues, and yet
they cannot appear on the left-hand side of an assignment.
An expression that can appear in the right-hand side of an
expression (but not in the left-hand side of an expression)
is an rvalue. For example:
#include <string>
using namespace std;
int& f();
void func()
{
int n;
char buf[3];
n = 5; // n is an lvalue; 5 is an rvalue
buf[0] = 'a'; //buf[0] is an lvalue, 'a' is an rvalue
string s1 = "a", s2 = "b", s3 = "c"; // "a", "b", "c" are
rvalues
s1 = // lvalue
s2 +s3; //s2 and s3 are lvalues that are implicitly
converted to rvalues
s1 =
string("z"); // temporaries are rvalues
int * p = new int; // p is an lvalue; 'new int' is an
rvalue
f() = 0; // a function call that returns a reference is
an lvalue
s1.size(); // otherwise, a function call is an rvalue
expression
}
An lvalue can appear in a context that requires an rvalue;
in this case, the lvalue is implicitly converted to an
rvalue. An rvalue cannot be converted to an lvalue.
Therefore, it is possible to use every lvalue expression in
the example as an rvalue, but not vice versa.
| Is This Answer Correct ? | 3 Yes | 1 No |
How do you define CONSTANT in C?
What are the features of the c language?
Write a function to find the area of a triangle whose length of three sides is given
write a progam to compare the string using switch case?
What is wrong with this initialization?
What is encapsulation?
How does sizeof know array size?
Explain function pointer with exapmles.
can we have joblib in a proc ?
How can I copy just a portion of a string?
Write a function that accepts a sentence as a parameter, and returns the same with each of its words reversed. The returned sentence should have 1 blank space between each pair of words. Demonstrate the usage of this function from a main program. Example: Parameter: “jack and jill went up a hill” Return Value: “kcaj dna llij tnew pu a llih”
f1() { f(3);} f(int t) { switch(t); { case 2: c=3; case 3: c=4; case 4: c=5; case 5: c=6; default: c=0;} value of c?