Lvalue and Rvalue in C
0 513
In C, lvalue and rvalue are terms used to classify expressions based on their ability to appear on the left-hand side (lvalue) or right-hand side (rvalue) of an assignment operation.
Lvalue
An lvalue refers to an object that occupies some identifiable location in memory (i.e., it has an address).
An lvalue can be used as both the left-hand side and the right-hand side of an assignment operation.
Simply put, an lvalue is something that can stand on the left side of an assignment.
An expression representing an object in memory that can be assigned a value.
It's something that has an identifiable memory address.
Lvalue Example:
// Program for Lvalue in C #include<stdio.h>int main() { int x; // Declaration of an integer variable int *ptr = &x; // Declaration of an integer pointer and assigning the address of x to it // Assigning a value to the variable x x = 10; // x is an lvalue here, as it can appear on the left side of an assignment // Printing the value of x printf("Value of x: %d\n", x); // Assigning a new value to x using the pointer *ptr = 20; // *ptr is an lvalue here, as it refers to the memory location of x // Printing the new value of x printf("New value of x: %d\n", x); return 0; }
Output:
Value of x: 10 New value of x: 20
Rvalue
An rvalue refers to a data value that is stored at some address in memory or a temporary value that doesn't necessarily have an address.
Rvalues can only appear on the right-hand side of an assignment operation.
In other words, an rvalue is something that can only be on the right side of an assignment.
An expression representing a value, but not necessarily an object in memory.
It's typically used on the right-hand side of an assignment.
Rvalue Program:
// Program for Rvalue in C #include <stdio.h>int main() { int a = 10, b = 20; // Declaration and initialization of two integer variables // Using rvalues in expressions int sum = a + b; // a + b is an rvalue expression, representing the sum of a and b // Printing the sum of a and b printf("Sum of a and b: %d\n", sum); // Another example of an rvalue int product = a * b; // a * b is an rvalue expression, representing the product of a and b // Printing the product of a and b printf("Product of a and b: %d\n", product); return 0; }
Output:
Sum of a and b: 30 Product of a and b: 200
In this program, a + b and a * b are rvalues because they represent values resulting from expressions, but they are not objects that can be assigned values.
// Program for lvalue and rvalue in C #include <stdio.h>int main() { // Example of lvalue: variable a int a = 5; // 'a' is an lvalue printf("Value of a: %d\n", a); // 'a' is an rvalue here // Example of rvalue: constant 10 int b = 10; // '10' is an rvalue printf("Value of b: %d\n", b); // 'b' is an lvalue here // Example of lvalue: array element int arr[5]; arr[0] = 20; // 'arr[0]' is an lvalue // Example of rvalue: result of an expression int x = 3, y = 4; int sum = x + y; // 'x + y' is an rvalue, it's the result of an expression return 0; }
Output:
Value of a: 5 Value of b: 10
In this program:
a, b, and arr[0] are all lvalues because they represent objects that have an identifiable memory location.
10, x + y, and arr[0] (when used in expressions) are all rvalues because they represent values rather than locations in memory.
Understanding lvalues and rvalues is essential, especially when dealing with pointers, references, and assignments in C.
In this program, x and *ptr are lvalues because they represent objects in memory that can be assigned values.
Share:
Comments
Waiting for your comments