. Advertisement .
..3..
. Advertisement .
..4..
To get around the “lvalue required as left operand of assignment” error, you will need to understand the concept of lvalue expressions in C and C++ . This article is going to help you with that.
What Are Lvalues In C And C++?
Every expression in C and C++ (such as a variable name, a constant, a function call, or an operator) has two properties associated with it: a value category and a type.
It is easy to understand the type, but what is the value category? It determines the nature of the expression and how you can use it in C and C++. We are going to only detail value categories in C here, but you should also be able to understand the same concepts in C++.
There are 3 value categories in C: function designator, rvalue, and lvalue. Lvalue is an expression that evaluates to an object identity. The name of lvalue reflects its most popular use: the operand on the left side of an assignment operator.
Example:
int i;
i = 10;
The assignment operation on the second line has i as an lvalue expression. It yields the reference of the object i (an int variable).
In C, lvalue expressions can also be used as the operand of:
- Compound assignment operators
- Member access operator
- Decrement and increment operators
- Address-of operator.
The “lvalue required as left operand of assignment” Error
Your C or C++ compiler will print the error message above when your code fails to provide an lvalue expression as the left operand of an assignment operator. This is a common mistake of many beginners, who may forget this rule in many situations.
One of the most common sources of this error is when people mistake the assignment operator “=” for the comparison operator “==”.
This is a program that checks whether a number is a multiple of another integer:
#include <stdio.h>
int main() {
int a;
int b;
printf("Enter the number a: ");
scanf("%d", &a);
printf("Enter the number b: ");
scanf("%d", &b);
if (a % b = 0) {
printf("a is a multiple of b");
}
else {
printf("a is not a multiple of b");
}
return 0;
}
Log:
lvalue.c: In function ‘main’:
lvalue.c:11:15: error: lvalue required as left operand of assignment
11 | if (a % b = 0) {
| ^
The error is produced because of the type above: you should use (a % b == 0) instead. The program should run without an issue now:
Enter the number a: 4
Enter the number b: 2
a is a multiple of b
This is another example:
#include <stdio.h>
int main() {
int a = 1;
int b = 0;
if (a = 0 && b = 0) {
printf("a and b are both zeroes.");
}
else {
printf("Either a or b is not zero.");
}
return 0;
}
Output:
lvalue.c:7:20: error: lvalue required as left operand of assignment
7 | if (a = 0 && b = 0) {
| ^
Because we have use = instead of ==, the && operator takes precedence over them. The compiler will evaluate the expression to this:
a = (0 && b) = 0
Which equals to:
a = true = 0
Since true isn’t an lvalue, the compiler prints out the error.
In this program, we attempt to overwrite the address of a variable through its pointer:
int main() {
int x=3;
int y=4;
&y=&x;
return 0;
}
But we end up with the same error:
lvalue.c: In function 'main':
lvalue.c:4:7: error: lvalue required as left operand of assignment
4 | &y=&x;
| ^
The reason is that the address of y (&y) is assigned statically and can’t be changed later during run time. You can only change what is stored at that address – not the address itself. This is an impossible task in C and C++.
Conclusion
The “lvalue required as left operand of assignment” error is the result of the wrong value type of the assignment operator in C and C++. It can occur even when you don’t even intend to use the assignment operation.
Leave a comment