In: Computer Science
Describe r-value and l-value. Can a r-value be a l-value? Can a l-value be a r-value? Use examples.
r-value:
r-value is an expression that is stored at some memory location and it can't have a value assigned to it.
This can be on the right-hand side of the assignment operator but not on the left-hand side of the assignment operator.
For example:
int a, b;
b = 10;
a = b + 5;
Here in the third statement, the expression 'b+5' is an r-value.
l-value:
l-value is an object that refers to some memory location and it can have a value assigned to it.
This can be on the left-hand side or right-hand side of the assignment operator.
For example:
int a, b;
a = 10;
Here in the second statement, 'a' is an l-value.
b = a;
In the above statement, 'l-value' appearing on right-hand side fo the assignment operator.
Can an r-value be a l-value?
No, because an r-value is an expression and an expression is not allowed in the left-hand side of the assignment operator.
For example:
int a;
10 = a; //Not allowed
Can a l-value be an r-value?
Yes, l-value is an object that refers to some memory location and it can have a value assigned to it.
For example:
For example:
int c, d;
c = 20;
Here in the second statement, 'c' is an l-value.
d = c;
In the above statement, 'l-value' appearing on right-hand side fo the assignment operator.