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

What are pointers really good for, anyway?

0 Answers  


Is there something we can do in C but not in C++? Declare variable names that are keywords in C++ but not C.

2 Answers   Infosys,


What is extern c used for?

0 Answers  


what is diff b/w huge & far & near pointer??

1 Answers   HCL,


What is the use of the function in c?

0 Answers  






#define MAX 3 main() { printf("MAX = %d \n",MAX ); #undef MAX #ifdef MAX printf("Vector Instituteā€); #endif

4 Answers   IBM, Vector,


What is nested structure?

0 Answers  


What is volatile

2 Answers  


Why does everyone say not to use scanf? What should I use instead?

0 Answers  


write a c program to find the probability of random numbers between 1-1000

0 Answers   ADS,


how will you write a program on linked lists using JAVA programming???????????

1 Answers   Keane India Ltd,


largest Of three Number using without if condition?

0 Answers  


Categories