Forums.Sureshkumar.net : A Perfect Place to Share Knowledge         Blogs     Games    Magazines    

"Sharing knowledge does not lessen your store, often it gets you more. Sharing plays a key role in relationships and bonding, happens in small steps and is assisted through community membership."

Go Back   SURESHKUMAR.NET FORUMS > Interview Questions > Interview Questions > C
Register FAQ Members List Calendar Games Blogs Search Today's Posts Mark Forums Read

C C Interview Questions asked in various Interviews. Professionals are invited to share Answers for these C Interview Questions. C Interview Questions with answers, Experiences, Career Advices, Tips and more.

   

Reply
 
LinkBack Thread Tools Rate Thread Display Modes
Old 14-06-08, 02:30 AM   #1 (permalink)
Senior Member
 
Join Date: Apr 2008
Posts: 13,563
Thanks: 0
Thanked 4 Times in 4 Posts
Thanks: 0
Thanked 4 Times in 4 Posts
Rep Power: 136 ShivaniX is on a distinguished road
What do "lvalue" and "rvalue" mean

What do "lvalue" and "rvalue" mean?

ShivaniX is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 26-06-08, 05:26 PM   #2 (permalink)
Junior Member
 
Join Date: Apr 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 1 mohanl is on a distinguished road
Post Re: What do "lvalue" and "rvalue" mean

lvalue" is an expression that refers to an object (which can have (a) value(s) within it), and "rvalue" is an expression that only has a value and no association with an object.

Basically lvalue is a storage location where data can be stored/ assigned and moreover it is not an expression. rvalue is an expresion which has a value. all lvalues can be used as rvalues. But all rvalues can't be used as lvalues.

lvalue means expression that can be used on the left hand side of an assignment.
All lvalues can be used as rvalues. Reverse isn't always true.

An "L value" is an expression that represents a storage location. The name "L value" comes from the fact that such expressions can go on the left-hand side of an assignment.

In this example:

int p*;
double a[20];
double v;

"p", "v", "(p++)*", "(10+a)*", and "a[12]" are all "L values" because those expressions are all valid on the left-hand side of an assignment (before the "=").

However, "sqrt(v)", "a[12]+5.0", "(10+a)* *10.0", and "(p++)*/v" are not "L values", because, although they are syntactically correct C expressions, they do not represent a storage location and therefore cannot go on the left-hand side of an expression. Such expressions are called "R values", because in an assignment they can only be used on the right hand side.
mohanl is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 29-10-08, 01:45 PM   #3 (permalink)
Junior Member
 
Join Date: Oct 2008
Age: 24
Posts: 14
Thanks: 2
Thanked 0 Times in 0 Posts
Thanks: 2
Thanked 0 Times in 0 Posts
Rep Power: 1 retheeshsoman is on a distinguished road
Re: What do "lvalue" and "rvalue" mean

Dear Sivani,
When we declare a variable we inform the compiler of two things, the name of the variable and the type of the variable. For example, we declare a variable of type integer with the name k by writing:
int k;
On seeing the "int" part of this statement the compiler sets aside 4 bytes of memory (on a PC) to hold the value of the integer. It also sets up a symbol table. In that table it adds the symbol k and the relative address in memory where those 4 bytes were set aside.
Thus, later if we write:
k = 2;
we expect that, at run time when this statement is executed, the value 2 will be placed in that memory location reserved for the storage of the value of k. In C we refer to a variable such as the integer k as an "object".

In a sense there are two "values" associated with the object k. One is the value of the integer stored there (2 in the above example) and the other the "value" of the memory location, i.e., the address of k. Some texts refer to these two values with the nomenclature rvalue (right value, pronounced "are value") and lvalue (left value, pronounced "el value") respectively.

In some languages, the lvalue is the value permitted on the left side of the assignment operator '=' (i.e. the address where the result of evaluation of the right side ends up). The rvalue is that which is on the right side of the assignment statement, the 2 above. Rvalues cannot be used on the left side of the assignment statement. Thus: 2 = k; is illegal.

Actually, the above definition of "lvalue" is somewhat modified for C. According to K&R II (page 197): [1]

"An object is a named region of storage; an lvalue is an expression referring to an object."

However, at this point, the definition originally cited above is sufficient. As we become more familiar with pointers we will go into more detail on this.

Okay, now consider:

int j, k;

k = 2;
j = 7; <-- line 1
k = j; <-- line 2

In the above, the compiler interprets the j in line 1 as the address of the variable j (its lvalue) and creates code to copy the value 7 to that address. In line 2, however, the j is interpreted as its rvalue (since it is on the right hand side of the assignment operator '='). That is, here the j refers to the value stored at the memory location set aside for j, in this case 7. So, the 7 is copied to the address designated by the lvalue of k.

In all of these examples, we are using 4 byte integers so all copying of rvalues from one storage location to the other is done by copying 4 bytes. Had we been using two byte integers, we would be copying 2 bytes.
retheeshsoman is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Tags
c language , interview questions



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT +6.5. The time now is 06:52 AM.





Search Engine Optimization by vBSEO 3.1.0