## Saturday, February 26, 2011

### lvalue and rvalue.....

This topic is really confusing to the beginners as well a to the intermediate programmers as they dun mess with this error usually.......but it is very necessary to understand what they are and when they come in the program????
let us try to understand what is  an lvalue???
K&R Says:
"An object is a manipulatable region of storage; an lvalue is an expression referring to an object....The name 'lvalue' comes from the assignment expression E1 = E2 in which the left operand E1 must be an lvalue expression"

so,we can say that::
LValue must be a container i.e. which have ability to hold the data and in c there is one container and which is variable. Hence LValue must be a variable in c.It cannot be a
-->constant,
-->function,
--> or any other data type of c.

lvalue-->location value
rvalue-->read value

Note-->For the assignment to be valid, the left operand must refer to an object that is it must be an lvalue. The right operand can be any expression. It need not be an lvalue.
For example:
int p;
declares p as an object of type int. When you use p in an assignment expression such as:
p=7;
here p is an expression (a subexpression of the assignment expression) referring to an int object. The expression p is an lvalue.
Suppose you switch the left and right operands:
7=p;
Here the compiler will generate the errror.Although 3 is an expression but it is not a lvalue beacuse a constant can't be a lvalue rather it is an rvalue because it does not represent an object it just represent a value........

What is an rvalue???
rvalue is described as the value of an expression.....
more interestingly::lvalues are the subset of rvalues...that is all lvalues are rvalues but all rvalues are not lvalues....
An rvalue can be:
-->any C constant
-->any variable
-->function which returns a value
we can understand lvalue errors by generating them in our programs::so here are some question with explanation which generates lvalue required error;
1.)
void main()
{
int a=10;
7=a;
7=10;
}
Explanation:an integer constant can't be an lvalue...
2.)
void main()
{
const b=55;
b=70;
}
Explanation:a constant can't be an lvalue.
3.)
struct student
{
char *name;
int age;
}stu;
void main()
{
stu={"milan",21};
}
Explanation::Data types other than basics can't be lvalue..
4.)
#define VAL 10
void main()
{
VAL=50;
}
Explanation:macro constants can't be an lvalue...
5.)
enum {ONE,TWO};
void main()
{
TWO=2;
}
Explanation:enum constant can't be lvalue;

Now have a look at Rvalue:
#include<stdio.h>
#define num 10
void show();
float add();
enum {KING,QUEEN};
int main(){
int x=2; //RValue can be a constant.
float y=9.0;
x=num; //RValue can be a macro constant.
x=KING; //RValue can be a enum constant.
y=add();//RValue can be a function which return a value
y=show(); //RValue can't be a function which does not return any value
return 0;
}
Explanation::you can explain easily now.
more examples in the next post..........

#### 3 comments:

1. really very good...

2. nice expln. Is there any expln like Left value for Lvalue and Right value for Rvalue

3. Feel free to comment......