Reputation: 323
can anyone give me a proper explanation about lvalues and rvalues in c. I havent found a proper one. if anyone has a link can u please paste t as a comment.
Upvotes: 2
Views: 443
Reputation: 1
lvalues (left-values) are (usually) found on the left side of the equality "=" operator.
rvalues (right-values) are found on the right side of the equality "=" operator.
lvalues are objects located in a memory location (address). lvalues get deleted once the block in which they were declared gets terminated.
rvalues are temporary values that get deleted as soon as they are initialized or assigned to an lvalue.
Ex:int a = 1 + 2;
In the example above, "a" is the lvalue since it is in the left side of the equality operator and the expression "1 + 2" is the rvalue since it is in the right side of the equality operator. Once this statement is executed, the variable "a" (now initialized to 3) persists in memory, while the expression "1 + 2" gets automatically deleted.
Here is a link: https://msdn.microsoft.com/en-us/library/f90831hc.aspx
Upvotes: 0
Reputation: 206518
l-values are entities whose address (usually exceptions: register variables)can be taken & they reside in memory long enough to be referred to by an name.
r-values are variables objects whose address cannot be taken & they do not reside in memory long enough to be referred to by an name, they are nameless. Often also called as nameless temporary objects.
int returnanInteger()
{
int i = 10;
return i;
}
void doSomething(int i)
{
//do something, right now we don't care
}
int main()
{
int i = 20;
doSomething(returnanInteger());
return 0;
}
In above program i
in main()
is an l-value because its address can be taken and it can be referred by an name.
While, in the statement:
doSomething(returnanInteger());
The value returned by returnanInteger()
is an r-value, it's address cannot be taken and it does not remain in memory long enough to be referred by nay name.It is immediately copied to be passed as an argument to the function doSomething()
.
Upvotes: 2
Reputation: 78903
A simplification would be to see lvalues as typed "objects" that have an explicit allocation in the program, either as variables, compound literals or through malloc
or similar mechanisms. Depending on their type, lvalues can be modifiable or not (specified with const
for the type), and you may be allowed to take the address of it or not (specified with register
for the declaration of variables).
An rvalue is just a "value" that the program produces as an intermediate step in an evaluation, and for which the compiler may freely choose the particular representation that suits best. Rvalues may not be modified and their address can usually not be taken. (There are very special circumstance that allow to take it for array component evaluation.)
Upvotes: 0
Reputation: 8136
All the values on the right side of = ("equal" to sign in c programming) are rvalues.Like x = 23; c = 'x'; int *ptr = &x;
here 23 , 'x' and '&x" are the rvalues Correspondingly x , c and *ptr are the lvalues.They represent the variables or objects in the memory You can never put the rvalues on the left side of = ("equal" to sign in c programming)
See this link - http://www.devx.com/tips/Tip/5696 for more info. rgds Softy
Upvotes: 0