{ }codelper

Error Messages › C

C lvalue required as left operand of assignment

error: lvalue required as left operand of assignment

This error means the left side of = is something you can't store a value in. An lvalue is something that has a place to hold a value, like x or a[i]. A calculation result or a number, such as x + 1, x % 2 or 10, has no place to put a value. Nine times out of ten you typed = where you meant ==, which asks whether two things are equal, so start with the = on the line with the error.

Cause 1. You wrote = for ==

= means "store this", and == means "are these equal?". if (x % 2 = 0) reads as "store 0 in x % 2", but x % 2 is only the result of a calculation, not a place to store a value, so you get the error.

Wrong code

#include <stdio.h> int main(void) {    int x = 6;    if (x % 2 = 0) {        printf("even\n");    }    return 0;}
When you run it
main.c: In function ‘main’:
main.c:5:15: error: lvalue required as left operand of assignment
    5 |     if (x % 2 = 0) {
      |               ^

Edit and run it →

Fixed code

Use == when you are asking whether two things are equal.

#include <stdio.h> int main(void) {    int x = 6;    if (x % 2 == 0) {        printf("even\n");    }    return 0;}
Output
even

Edit and run it →

When the left side is a single variable (if (x = 0)), you don't get this error, because that is a place you can store into. You get the warning suggest parentheses around assignment used as truth value instead, and the condition does not behave the way you meant. That warning is also a sign to change it to ==.

Cause 2. You swapped the two sides

In math you can write a + b = sum, but = in C is a command that stores the value on the right into the variable on the left. If a calculation is on the left, there is nowhere to store it.

Wrong code

#include <stdio.h> int main(void) {    int a = 3, b = 4;    int sum;    a + b = sum;    printf("%d\n", sum);    return 0;}
When you run it
main.c: In function ‘main’:
main.c:6:11: error: lvalue required as left operand of assignment
    6 |     a + b = sum;
      |           ^

Edit and run it →

Fixed code

Put the variable that receives the value on the left and the calculation on the right.

#include <stdio.h> int main(void) {    int a = 3, b = 4;    int sum;    sum = a + b;    printf("%d\n", sum);    return 0;}
Output
7

Edit and run it →

Cause 3. You used = together with && or ||

if (a == 1 && b = 2) looks fine because both sides are variables, but it gives the same error. = is evaluated after &&, so the compiler reads this as (a == 1 && b) = 2. The left side has become the result of a calculation.

Wrong code

#include <stdio.h> int main(void) {    int a = 1, b = 2;    if (a == 1 && b = 2) {        printf("both\n");    }    return 0;}
When you run it
main.c: In function ‘main’:
main.c:5:21: error: lvalue required as left operand of assignment
    5 |     if (a == 1 && b = 2) {
      |                     ^

Edit and run it →

Fixed code

Change it to == here too.

#include <stdio.h> int main(void) {    int a = 1, b = 2;    if (a == 1 && b == 2) {        printf("both\n");    }    return 0;}
Output
both

Edit and run it →

What to check when you see this error

  1. Find every single = on the line with the error. If it is inside the parentheses of an if or while, it should be ==.
  2. Check that the left side of = is a single variable (or a[i], *p). A calculation, a number or a function call can't go on the left.
  3. If it is a statement that stores a value, check that the two sides are not swapped. The receiving side goes on the left.
  4. If you try to assign to an array name, as in name = "kim";, you get a similar but different error (assignment to expression with array type). Copy a string with strcpy.
  5. The message differs between compilers. clang says expression is not assignable, and Visual Studio says error C2106 (left operand must be l-value).

Practice problems

See also