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..........



Wednesday, February 23, 2011

A question on main.......

A few days back while playing with the programs i got stucked at the following program::

void (main(main))
{
printf("main=%d",++main);
}

that was the multiple choice question and there was an option compilation fails ,i smartly tick that and moved on to the next question ,but after doing all the question when i was checking my result i got 6/7 then i saw what went wrong because according to me the questions were simple....then i ran this on the machine instead of my mind and amazingly got that compiled without any error  and the output was:
main=2

I was amazed by seeing the answer,so started to find how it compiles,then i checked with other functions also and got the reason why void (main()) compiles without any error,but the solution of this question was the mystery again that is why void (main(main)) compiles....then i tried further to get the answer.......
after a little exploration i came up with the common misconception that main() does not accept any argument instead it can take any number of arguments.
then i take a lok at the standard for the main declaration and found this..
then i got why void (main(abc)),void main(main(abc,def,ghi,jkl)) or with any number arguments compiles under C89 because under C89 main() is acceptable and can take any number of arguments....
and i knew that main can be used as a variable name or identifier and is not treated as a keyword....so i come to know that  why void (main(main)) compiles??? because main can take any number of arguments under C89 and main can be used as a variable name or identifier ....

then again i stucked why the output is 2 that is why main equals to 1 ........then i tried the following code:

void (main(a,b,c,d))
{
printf("a=%d,b=%d,c=%d,d=%d",a,b,c,d);
}

and the output is:
a=1,b=garbage,c=garbage,d=garbage

Then i tried that on three different compilers that is turboc,dev and gcc on all the compilers it gives 1 for the first argument and garbage for the others the reason for this is still a mystery for me ....
anybody if knows why the first argument value is always 1 please post the reason as comment to this post.......and if you found this useful please rate this post.........