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
                                                                       --> 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:
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:
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;
void main()
int a=10;
Explanation:an integer constant can't be an lvalue...
void main()
const b=55;
Explanation:a constant can't be an lvalue.
struct student
char *name;
int age;
void main()
Explanation::Data types other than basics can't be lvalue..
#define VAL 10
void main()
Explanation:macro constants can't be an lvalue...
enum {ONE,TWO};
void main()
Explanation:enum constant can't be lvalue;

Now have a look at Rvalue:
#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..........


  1. really very good...

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


Feel free to comment......