What is the Lvalue and Rvalue?

Answers were Sorted based on User's Feedback



What is the Lvalue and Rvalue?..

Answer / vadivel t

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

What is the Lvalue and Rvalue?..

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

Post New Answer

More C Interview Questions

How to set a variable in the environment list?

1 Answers  


what is the answer for it main() { int i; clrscr(); printf("%d",&i)+1; scanf("%d",i)-1; }

3 Answers  


2.Given the short c program that follows a. make a list of the memory variables in this program b.which lines of code contain operations that change the contents of memory? what are those operations? Void main( void) { Double base; Double height; Double area; Printf(“enter base and height of triangle :”); Scanf(“%lg”, &base); Scanf(“%lg”, &height); Area=base*height/2.0; Printf(“the area of the triangle is %g \n”,area); }

1 Answers   Wipro,


How does pointer work in c?

0 Answers  


Explain how do you print only part of a string?

0 Answers  






int far *near * p; means

0 Answers   Honeywell,


which type of aspect you want from the student.

0 Answers   IBM, TCS,


a value that does not change during program execution a) variabe b) argument c) parameter d) none

0 Answers  


What is a good way to implement complex numbers in c?

0 Answers  


What is variables in c?

0 Answers  


Why does not c have an exponentiation operator?

0 Answers  


How do you write a program which produces its own source code as its output?

2 Answers  


Categories