In C programming, grasping the concepts of lvalue and rvalue is crucial for writing efficient and error-free code. These terms define how expressions are evaluated and how memory is accessed and manipulated. This tutorial aims to demonstrate the lvalues and rvalues in C, providing clear explanations, examples, and insights to enhance your understanding.​
What is an Lvalue?
An lvalue (locator value) refers to an object that occupies an identifiable location in memory. In simpler terms, an lvalue has an address that can be accessed using the address-of operator (&). Lvalues can appear on the left or right side of an assignment.​
Characteristics of Lvalues:
- Have a persistent memory address.
- Can appear on the left-hand side of an assignment.
- Can be modified if not declared as const.​
Examples of Lvalues:
int x = 30; // 'x' is an lvalue x = 20; // 'x' is on the left-hand side int *p = &x; // 'x' has an address
In the above examples, x is an lvalue because it refers to a specific memory location.​
What is an Rvalue?
An rvalue (read value) is a temporary value that does not have a persistent memory address. Rvalues are typically literals or the result of expressions and can only appear on the right-hand side of an assignment.​
Characteristics of Rvalues:
- Do not have a persistent memory address.
- Can appear only on the right-hand side of an assignment.
- Cannot be assigned a value.​
Examples of Rvalues:
int x = 40; // '40' is an rvalue int y = x + 5; // 'x + 5' is an rvalue
In these examples, 40 and x + 5 are rvalues because they do not refer to a specific memory location.​
Lvalue vs Rvalue: Key Differences
Aspect | Lvalue | Rvalue |
---|---|---|
Memory Address | Has a persistent address | No persistent address |
Assignment Position | Can be on both sides | Only on the right-hand side |
Modifiability | Modifiable (unless const) | Not directly modifiable |
Examples | Variables, array elements | Literals, temporary expressions |
L Value and R Value Examples in C
Example 1: Assigning Quiz Score
#include <stdio.h> int main() { int mcq = 10; int score; score = mcq + 5; // 'score' is L-value, 'mcq + 5' is R-value printf("Final score = %d\n", score); // Output: Final score = 15 return 0; }
This C program shows how L-values and R-values work. It sets mcq = 10 and then adds 5 to it. The result, 15, is stored in score (an L-value). mcq + 5 is an R-value because it’s a temporary value. The program prints: “Final score = 15”.
Example 2:
#include <stdio.h> int main() { int quiz = 8; int bonus = 2; quiz = quiz + bonus; // 'quiz' is L-value, 'quiz + bonus' is R-value printf("Updated quiz score = %d\n", quiz); // Output: 10 return 0; }
This C program shows how L-values and R-values are used in assignment. It sets quiz = 8 and bonus = 2. The expression quiz + bonus (an R-value) adds them to get 10. This value is stored back in quiz, which is an L-value. The program prints: “Updated quiz score = 10”.
Example 3: Illegal Assignment to R-value
#include <stdio.h> int main() { int mcq = 5; (mcq + 2) = 10; // Error: R-value on the left return 0; }
This C program shows an invalid assignment. It sets mcq = 5 and then tries to assign 10 to (mcq + 2). But (mcq + 2) is an R-value a temporary result so it can’t be on the left side of an assignment. This causes a compile-time error. The program won’t run until the error is fixed.
Example 4: Function Call with R-value
#include <stdio.h> void evaluate(int marks) { printf("Marks: %d\n", marks); } int main() { int score = 12; evaluate(score); // L-value used as R-value (score passed by value) evaluate(score + 3); // R-value (expression) directly return 0; }
This C program shows how L-values and R-values are used in function calls. It defines a function evaluate() that prints the given marks. In main(), score = 12 is passed to the function—score is an L-value used as an R-value. Then score + 3, an R-value (temporary result), is also passed.
Example 5:
#include <stdio.h> int main() { int quiz = 7; int *ptr = &quiz; // Valid: 'quiz' is L-value printf("Address of quiz = %p\n", (void*)ptr); return 0; }
This C program shows how to use an L-value with a pointer. It sets quiz = 7 and creates a pointer ptr that stores the address of quiz. Since quiz is an L-value, it has a valid memory address. The program then prints the address of quiz using the pointer, and ends.
Example 6: Invalid Address of R-value
#include <stdio.h> int main() { int *ptr = &(3 + 4); // Error: cannot take address of R-value return 0; }
Common Misconceptions
Misconception 1: All variables are lvalues.
While most variables are lvalues, certain expressions involving variables can result in rvalues. For example, the result of a function returning a non-reference type is an rvalue.​
Misconception 2: Rvalues cannot be assigned.
Rvalues can be assigned to lvalues, but rvalues themselves cannot be assigned a value. For instance, x = 5; is valid, but 5 = x; is invalid because 5 is an rvalue.​
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Check C Books
- Check Computer Science Books
- Apply for C Internship
- Watch Advanced C Programming Videos
- Apply for Computer Science Internship