Compilation (gcc error)Compiler Syntax6 min read

Fix “lvalue required as left operand of assignment” in C

In C, an assignment operator requires a modifiable object locator (an lvalue) on its left-hand side. Temporary expression results (rvalues), arithmetic expressions, macro constants, and array names do not identify writable storage you can reassign directly.

Quick Fix Summary

Replace accidental single = inside conditions with ==, write results into a named variable instead of (a + b) or ternary expressions, and copy into arrays with snprintf or memcpy.

Why This Error Happens

Why expressions cannot sit on the left of =

When the compiler evaluates x + 1 or a % 2, the result exists only as a transient value in a register or temporary slot with no addressable C object to mutate.

Common C triggers

Writing if (x % 2 = 0) attempts to assign 0 to the arithmetic rvalue x % 2. Similarly, ternary expressions (cond ? a : b) = 1 yield a value, not a reference as in C++.

Top Root Causes & Specific Code Fixes

Using = instead of == inside a compound condition

Arithmetic expressions like x % 2 produce rvalues that cannot be assigned.

Broken Pattern
if (x % 2 = 0) {
    puts("even");
}
Corrected Pattern
if (x % 2 == 0) {
    puts("even");
}
Illustrative diagnostic (not executed):error: lvalue required as left operand of assignment

Assigning directly to a ternary conditional expression

Unlike C++, C ternary expressions never evaluate to an lvalue.

Broken Pattern
(choose_a ? a : b) = 10;
Corrected Pattern
*(choose_a ? &a : &b) = 10;

Complete Broken and Corrected Programs

Compare the source before running it. The broken program is intentionally incorrect; its behavior can depend on the compiler and platform.

Read the broken program
#include <stdio.h>
int main(void) {
    int x = 4;
    if (x % 2 = 0) {
        printf("even\n");
    }
    return 0;
}
Open broken program in compiler
Read the corrected program
#include <stdio.h>
int main(void) {
    int x = 4;
    if (x % 2 == 0) {
        printf("even\n");
    }
    return 0;
}
Open corrected program in compiler

Expected output from the corrected program

Reference output, not a live execution result.

even

Compiler & Sanitizer Diagnostic Matrix

Compiler / Runtime SymptomRoot CauseDiagnostic Flag / CheckCorrected Pattern
lvalue required in `if (x % 2 = 0)`Single `=` instead of `==` after arithmetic-Wall -WparenthesesUse equality comparison `x % 2 == 0`
lvalue required on `(c ? a : b) = 1`Ternary yields rvalue in ISO CUse pointer dereferenceWrite `*(c ? &a : &b) = 1;`

▶ Load failing reproducer in Online C Compiler

Debugging Techniques and Commands

Compiler Warnings
gcc -std=c17 -Wall -Wextra -Wparentheses main.c -o app

Flags suspicious assignments inside truth conditions immediately.

Clang Diagnostics
clang -std=c17 -Wall -Wextra main.c -o app

Highlights the exact non-lvalue span with fix-it hints.

Frequently Asked Questions

Why does C reject (cond ? a : b) = 5 when C++ accepts it?

ISO C defines the conditional operator result as a value rather than an lvalue; dereferencing `*(cond ? &a : &b)` works in both C and C++.

Can const variables trigger lvalue errors?

Const variables produce `assignment of read-only variable`, which is a non-modifiable lvalue error closely related to lvalue rules.

How do I prevent = vs == typos in conditions?

Compile with `-Wall` and treat `-Wparentheses` warnings as errors during development.