How To Fix “error: lvalue required as left operand of assignment”

Error: lvalue required as left operand of assignment

The message “error: lvalue required as left operand of assignment” can be shown quite frequently when you write your C/C++ programs. Check out the explanation below to understand why it happens.

l-values And r-values

In C and C++, we can put expressions into many categories, including l-values and r-values

The history of these concepts can be traced back to Combined Programming Language. Their names are derived from the sides where they are typically located on an assignment statement.

Recent standards like C++17 actually define several categories like xvalue or prvalue. But the definitions of l-values and r-values are basically the same in all C and C++ standards.

In simple terms, l-values are memory addresses that C/C++ programs can access programmatically. Common examples include constants, variable names, class members, unions, bit-fields, and array elements.

In an assignment statement, the operand on the left-hand side should be a modifiable l-value because the operator will evaluate the right operand and assign its result to the left operand.

This example illustrates the common correct usage of l-values and r-values:

int main() {
   int x, *p;
   x = 4;
   x++;
   *p = x;
   return 0;
}

In the ‘x = 4’ statement, x is an l-value while the literal 4 is not. The increment operator also requires an l-value because it needs to read the operand value and modify it accordingly.

Similarly, dereferenced pointers like *p are also l-values. Notice that an l-value (like x) can be on the right side of the assignment statement as well.

Causes And Solutions For “error: lvalue required as left operand of assignment”

C/C++ compilers generates this error when you don’t provide a valid l-value to the left-hand side operand of an assignment statement. There are many cases you can make this mistake.

Example 1

This code can’t be compiled successfully:

#include <iostream>
using namespace std;
int main() {
   int x;
   4 = x;
   cout << x;
   return 0;
}

Output:

example1.cpp: In function ‘int main()’:
example1.cpp:6:4: error: lvalue required as left operand of assignment
    6 |    4 = x;
      |    ^

As we have mentioned, the number literal 4 isn’t an l-value, which is required for the left operand. You will need to write the assignment statement the other way around:

#include <iostream>
using namespace std;
int main() {
   int x;
   x = 4;
   cout << x;
   return 0;
}

Output:

4

Example 2

In the same manner, this program won’t compile either:

int main() {
   int x;
   x = 4;
   x + 1 = x;
   cout << x;
   return 0;
}

In C/C++, the ‘x + 1’ expression doesn’t evaluate to a l-value. You can fix it by switching the sides of the operands:

int main() {
   int x;
   x = 4;
   x = x + 1;
   cout << x;
   return 0;
}

Output:

5

Example 3

This is another scenario the compiler will complain about the left operand:

int main() {
   int x;
   (-x) = 4;
   cout << x;
   return 0;
}

(-x) doesn’t evaluate to a l-value in C/C++, while ‘x’ does. You will need to change both operands to make the statement correct:

int main() {
   int x;
   x = -4;
   cout << x;
   return 0;
}

Output:

-4

Example 4

Many people also use an assignment operator when they need a comparison operator instead:

#include <iostream>
#include <string.h>
using namespace std;
int main() {
   char str1[] = "LearnShareIT";
   char str2[] = "LearnShareIT";
   if (strcmp (str1,str2) = 0) {
      cout << "Two strings are equal.";
   } else {
      cout << "Two strings are not equal.";
   }
   return 0;
}

This leads to a compilation error:

example4.cpp: In function ‘int main()’:
example4.cpp:8:15: error: lvalue required as left operand of assignment
    8 |    if (strcmp (str1,str2) = 0) {
      |        ~~~~~~~^~~~~~~~~~~

The if statement above needs to check the output of a comparison statement:

if (strcmp (str1,str2) == 0)

Output:

Two strings are equal.

Summary

C/C++ compilers will give you the message “error: lvalue required as left operand of assignment” when there is an assignment statement in which the left operand isn’t a modifiable l-value. This is usually the result of syntax misuse. Correct it, and the error should disappear.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *