🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →Invalid C Expressions Explained
Decode lvalue errors, the ternary trap and unsequenced modifications with real compiler output
Welcome back to our free C programming course. So far every expression we traced was valid. This lecture studies the opposite: expressions that the compiler refuses, and the sneakier ones it accepts but should not. Learning to read these failures is a core skill of embedded C programming, where a cryptic compiler message or a silent undefined behaviour can cost a day on real hardware.
We study four families: the lvalue errors, the ternary-assignment trap, unsequenced modifications, and comma and assignment slips. Every error message below was captured from real GCC 13.3 and Clang 18.1 runs. This free C tutorial finishes with a table that maps each message back to its cause, so you can decode them quickly.
What You Will Learn in This Free C Programming Course Lecture
- What an lvalue is and why
(a + b)++cannot compile - Why
a > b ? max = a : max = b;is an error in C - Three ways to fix the ternary assignment trap
- How unsequenced modifications compile with a warning and give different answers on different compilers
- How the comma operator and a stray
=insideifchange the meaning of code - How to decode GCC and Clang diagnostics
Prerequisites for This Free C Programming Course Lecture
- Increment, decrement, assignment and conditional operators
- The lectures on evaluation order and expression tracing in this chapter
What Is an lvalue and Why Does It Matter?
An lvalue is an expression that refers to an object in memory, something that has an address and can hold a value. A variable name is an lvalue. A constant, or the result of an arithmetic expression, is not: it is only a value, a temporary result with nowhere to be stored.
Three groups of operators demand an lvalue: the assignment family (=, += and so on) needs one on the left, the increment and decrement operators need one as their operand, and the address-of operator & needs one to take the address of.
| Expression | Valid? | Reason |
|---|---|---|
a++; | Yes | a is an object |
a = b = 3; | Yes | both a and b are objects |
5 = a; | No | 5 is a value, not an object |
(a + b)++; | No | the sum is a temporary value |
a + b = 7; | No | grouping makes it (a + b) = 7, a value on the left |
-a = 5; | No | unary minus produces a value |
a++ = 3; | No | a++ yields the old value, not the object |
++a++; | No | it parses as ++(a++) and a++ is not an lvalue |
&(a + b) | No | a temporary has no address |
Every row marked No was confirmed by compiling it. Here is a small file with three of them, and what each compiler says:
#include <stdio.h>
int main(void)
{
int a = 4, b = 6;
(a + b)++; /* error: result of a + b is not a variable */
(a + b) = 10; /* error: cannot assign to a value */
int m = 7++; /* error: 7 is not a variable */
printf("%d %d %d\n", a, b, m);
return 0;
}
$ gcc -std=c11 -Wall -Wextra e23_lvalue.c
src/e23_lvalue.c: In function 'main':
src/e23_lvalue.c:7:12: error: lvalue required as increment operand
7 | (a + b)++; /* error: result of a + b is not a variable */
| ^~
src/e23_lvalue.c:8:13: error: lvalue required as left operand of assignment
8 | (a + b) = 10; /* error: cannot assign to a value */
| ^
src/e23_lvalue.c:9:14: error: lvalue required as increment operand
9 | int m = 7++; /* error: 7 is not a variable */
| ^~
$ clang -std=c11 -Wall -Wextra e23_lvalue.c
src/e23_lvalue.c:7:12: error: expression is not assignable
7 | (a + b)++; /* error: result of a + b is not a variable */
| ~~~~~~~^
src/e23_lvalue.c:8:13: error: expression is not assignable
8 | (a + b) = 10; /* error: cannot assign to a value */
| ~~~~~~~ ^
src/e23_lvalue.c:9:14: error: expression is not assignable
9 | int m = 7++; /* error: 7 is not a variable */
| ~^
3 errors generated.
GCC uses the term lvalue required. Clang says expression is not assignable. They describe the same defect. Note that all three errors are reported in one run, so read the whole list before you start fixing.
The Ternary Assignment Trap
A very natural way to write a maximum looks like this:
#include <stdio.h>
int main(void)
{
int a = 10, b = 3, max;
a > b ? max = a : max = b;
printf("%d\n", max);
return 0;
}
$ gcc -std=c11 -Wall -Wextra e23_ternary_bad.c
src/e23_ternary_bad.c: In function 'main':
src/e23_ternary_bad.c:7:27: error: lvalue required as left operand of assignment
7 | a > b ? max = a : max = b;
| ^
$ clang -std=c11 -Wall -Wextra e23_ternary_bad.c
src/e23_ternary_bad.c:7:27: error: expression is not assignable
7 | a > b ? max = a : max = b;
| ~~~~~~~~~~~~~~~~~~~~~ ^
1 error generated.
The error points at the final =. In C the middle operand of ?: may be any expression, but the third operand must be a conditional-expression, which cannot contain a bare assignment. So the compiler groups the line as shown below:
Three fixes work. All three give the same answer, shown by this program:
#include <stdio.h>
int main(void)
{
int a = 10, b = 3, max1, max2, max3;
max1 = a > b ? a : b; /* preferred */
a > b ? (max2 = a) : (max2 = b); /* parentheses fix the grouping */
if (a > b) max3 = a; else max3 = b; /* plain if/else */
printf("%d %d %d\n", max1, max2, max3);
return 0;
}
$ gcc -std=c11 -Wall -Wextra e23_ternary_ok.c -o e23_ternary_ok && ./e23_ternary_ok
10 10 10
- Preferred: assign the result of the conditional,
max = a > b ? a : b; - Parentheses:
a > b ? (max = a) : (max = b);compiles and works, though it is harder to read - Plain if/else: the clearest option when each branch does more than one thing
Unsequenced Modifications That Compile Anyway
Some bad expressions compile with only a warning. The program below has three of them. Each reads or modifies a variable twice with no sequence point between.
#include <stdio.h>
int main(void)
{
int i = 3;
i = i++ + 1; /* i is modified twice, unsequenced */
printf("i = %d\n", i);
int arr[4] = {0, 0, 0, 0};
int k = 1;
arr[k] = k++; /* k is read and modified, unsequenced */
printf("arr = %d %d %d %d, k = %d\n", arr[0], arr[1], arr[2], arr[3], k);
int a = 2, b = 5;
printf("%d %d\n", a = b, a == b); /* a is modified and read, unsequenced */
return 0;
}
$ gcc -std=c11 -Wall -Wextra e23_unseq.c -o e23_unseq_gcc && ./e23_unseq_gcc
src/e23_unseq.c: In function 'main':
src/e23_unseq.c:6:7: warning: operation on 'i' may be undefined [-Wsequence-point]
6 | i = i++ + 1; /* i is modified twice, unsequenced */
| ~~^~~~~~~~~
src/e23_unseq.c:11:15: warning: operation on 'k' may be undefined [-Wsequence-point]
11 | arr[k] = k++; /* k is read and modified, unsequenced */
| ~^~
src/e23_unseq.c:15:25: warning: operation on 'a' may be undefined [-Wsequence-point]
15 | printf("%d %d\n", a = b, a == b); /* a is modified and read, unsequenced */
| ~~^~~
i = 4
arr = 0 0 1 0, k = 2
5 0
$ clang -std=c11 -Wall -Wextra e23_unseq.c -o e23_unseq_clang && ./e23_unseq_clang
src/e23_unseq.c:6:10: warning: multiple unsequenced modifications to 'i' [-Wunsequenced]
6 | i = i++ + 1; /* i is modified twice, unsequenced */
| ~ ^
src/e23_unseq.c:11:15: warning: unsequenced modification and access to 'k' [-Wunsequenced]
11 | arr[k] = k++; /* k is read and modified, unsequenced */
| ~ ^
src/e23_unseq.c:15:25: warning: unsequenced modification and access to 'a' [-Wunsequenced]
15 | printf("%d %d\n", a = b, a == b); /* a is modified and read, unsequenced */
| ^ ~
3 warnings generated.
i = 4
arr = 0 0 1 0, k = 2
5 1
The first two lines agree on both compilers, by luck. The third line does not: GCC printed 5 0 and Clang printed 5 1. In printf("%d %d\n", a = b, a == b) the comparison reads a while the assignment changes it, and nothing says which happens first. The behaviour is undefined, so both answers are acceptable and neither can be trusted.
Compiles is not the same as correct
All three lines compiled and ran. Only the warnings told you something was wrong. In firmware you often cannot see the output at all, so make sequence-point warnings fail the build.
Comma and Assignment Slips
The comma and assignment operators have the lowest precedence in C, which creates quiet surprises.
#include <stdio.h>
int main(void)
{
int a, b;
int g = 4, h = 9;
a = 5, 6; /* assignment first, then the 6 is thrown away */
b = (5, 6); /* comma inside parentheses, 6 is assigned */
printf("a = %d, b = %d\n", a, b);
printf("%d\n", (g, h)); /* one argument: the comma operator */
printf("%d %d\n", g, h); /* two arguments: just separators */
if (g = h) /* assignment, not comparison */
printf("branch taken, g is now %d\n", g);
return 0;
}
$ gcc -std=c11 -Wall -Wextra e23_comma_assign.c -o e23_comma_assign && ./e23_comma_assign
src/e23_comma_assign.c: In function 'main':
src/e23_comma_assign.c:8:10: warning: right-hand operand of comma expression has no effect [-Wunused-value]
8 | a = 5, 6; /* assignment first, then the 6 is thrown away */
| ^
src/e23_comma_assign.c:9:11: warning: left-hand operand of comma expression has no effect [-Wunused-value]
9 | b = (5, 6); /* comma inside parentheses, 6 is assigned */
| ^
src/e23_comma_assign.c:12:22: warning: left-hand operand of comma expression has no effect [-Wunused-value]
12 | printf("%d\n", (g, h)); /* one argument: the comma operator */
| ^
src/e23_comma_assign.c:15:9: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
15 | if (g = h) /* assignment, not comparison */
| ^
a = 5, b = 6
9
4 9
branch taken, g is now 9
| Line | What happens | Why |
|---|---|---|
a = 5, 6; | a gets 5, the 6 is discarded | assignment binds tighter than the comma, so it is (a = 5), 6 |
b = (5, 6); | b gets 6 | the parentheses make the comma operator the right side |
printf("%d\n", (g, h)); | prints 9 | one argument whose value is the last comma operand |
printf("%d %d\n", g, h); | prints 4 9 | here the commas are only separators between arguments |
if (g = h) | assigns 9 to g and takes the branch | assignment, not comparison. It is true because 9 is non-zero |
The classic if (g = h) mistake is the one that bites embedded programmers hardest, because it also overwrites the variable. GCC reports suggest parentheses around assignment used as truth value. If you really want an assignment in a condition, add a second pair of parentheses and a comment. Many teams prefer the habit of writing the constant on the left, as in if (9 == g), so a missing = becomes a compile error.
Decoding Compiler Messages
| Problem | GCC 13.3 says | Clang 18.1 says |
|---|---|---|
| Assigning to a value | lvalue required as left operand of assignment | expression is not assignable |
| Incrementing a value | lvalue required as increment operand | expression is not assignable |
| Unsequenced change | operation on 'x' may be undefined [-Wsequence-point] | multiple unsequenced modifications to 'x' [-Wunsequenced] |
| Assignment in a condition | suggest parentheses around assignment used as truth value [-Wparentheses] | using the result of an assignment as a condition without parentheses [-Wparentheses] |
| Useless comma operand | right-hand operand of comma expression has no effect [-Wunused-value] | left operand of comma operator has no effect [-Wunused-value] |
Common Mistakes and Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
Error pointing at the last = of a ternary | Assignment in the third operand | Assign the whole conditional instead |
Error on ++ or -- with a bracketed expression | Operand is not an lvalue | Apply the operator to the variable itself |
| Different output on two compilers | Unsequenced modification | Split into separate statements |
| Branch always taken | = instead of == | Compare with == and enable -Wall |
| Only first value of a list is used | Comma operator instead of a separator | Check the parentheses |
Best Practices, Performance and Security
- Build with
-Wall -Wextraand turn selected warnings into errors, for example-Werror=sequence-pointon GCC. - Write one assignment per statement. It costs no speed and removes a whole class of errors.
- Use the conditional operator to produce a value, never to perform assignments.
- Do not suppress a warning to make a build pass. Understand it first.
- Accidental assignment in a condition is a recognised security weakness, because it can bypass a check silently.
Key Takeaways From This Free C Programming Course Lecture
=,++,--and&need an lvalue, an actual object.- In C,
a > b ? max = a : max = b;does not compile. Writemax = a > b ? a : b;. - Unsequenced modifications compile with a warning and can give different answers per compiler.
- The comma has the lowest precedence, so
a = 5, 6;assigns 5. if (x = y)assigns. Use==to compare.
Practice Questions
Q1. Which of these compile: x = 3;, 3 = x;, (x)++;, (x + 1)++;?
x = 3; and (x)++; compile. A parenthesised variable is still an lvalue. The other two fail because 3 and x + 1 are not objects.
Q2. Rewrite n > 0 ? flag = 1 : flag = 0; so that it compiles.
flag = n > 0 ? 1 : 0; or simply flag = n > 0;, because a relational expression already yields 1 or 0.
Q3. What does int v = 1, 2; do?
It does not compile as intended. In a declaration the comma separates declarators, so the compiler expects a new variable name after it, and 2 is not one. Use int v = (1, 2); if you truly want the comma operator.
Interview Questions on Invalid Expressions
What is an lvalue?
An expression that designates an object with a memory location, so it can appear on the left of an assignment and be the operand of ++ and &.
Why does a > b ? x = 1 : x = 2; fail in C?
The third operand of ?: cannot contain a bare assignment, so the line groups as (a > b ? x = 1 : x) = 2, an assignment to a non-lvalue.
Why can the same undefined expression print different values on GCC and Clang?
The standard imposes no requirement, so each compiler is free to pick any evaluation, and they choose differently.
Conclusion
You can now read the most common C expression errors instead of fearing them. An lvalue error means you targeted a value instead of an object, the ternary error means the grouping is not what you wrote, an unsequenced warning means the answer depends on the compiler, and a comma or assignment slip means the lowest precedence operators surprised you. That completes the study of operators and expressions in this free C programming course. In the next chapter we move on to control flow, where these expressions become the conditions that steer your program.
Frequently Asked Questions
What does ‘lvalue required as left operand of assignment’ mean?
The left side of the = is not an object that can store a value, for example a constant or the result of an arithmetic expression.
Why does a > b ? max = a : max = b not compile in C?
The third operand of the conditional operator must be a conditional expression, so the compiler groups the line as (a > b ? max = a : max) = b, an assignment to a non-lvalue. Use max = a > b ? a : b.
Is there a free C programming course that explains compiler errors?
Yes. This free C programming course from EmbeddedPathashala shows the real GCC and Clang messages for each mistake and maps them to their causes.
What is the difference between a = 5, 6 and a = (5, 6)?
The first assigns 5 because assignment binds tighter than the comma. The second assigns 6, the value of the comma expression.
Why do GCC and Clang print different results for the same C code?
When the code has undefined behaviour, such as a variable modified and read without a sequence point, each compiler may legally produce a different result.
How do I stop writing = instead of == in an if?
Compile with -Wall so the compiler warns, and optionally write the constant first, like if (9 == g).
Topics Covered
Keep Going With the Course
Operators and expressions are complete. Control flow is next.

2 Comments