Assignment vs Comparison in C – The if (x = 0) Bug

x is 10 — clearly not zero — yet this if doesn’t do what it looks like it does. One missing = turns a comparison into an assignment, and it’s arguably the most famous typo in C. This question from our C Programming Quiz App checks whether you can spot it — and whether you know exactly what the buggy version does instead.

The Quiz Question

int x = 10;
if (x = 0)
    printf("yes");
else
    printf("no");

What is printed by this code?

  1. yes
  2. no
  3. Compile error
  4. Undefined behavior

The Correct Answer: no

x = 0 is an assignment, and in C an assignment is an expression whose value is the value assigned. So this line does two things: it destroys x (10 → 0), and it hands the if the value 0 — false. The else branch runs. Verified on gcc 13.3 and Apple clang 21:

$ gcc -ansi -Wall -Wextra assign.c && ./a.out
no | x is now 0

Note the second casualty: x itself. The bug doesn’t just take the wrong branch — it corrupts the variable for all the code that follows.

Why Each Wrong Answer Is Wrong

Why not “yes”?

“yes” is what the intended code would print if x were 0 — or what the buggy code prints when the assigned value happens to be nonzero. Change the typo to if (x = 5) and the assignment evaluates to 5, which is truthy:

$ ./a.out
yes | x is now 5

That’s the really vicious property of this bug: whether it prints “yes” or “no” depends on the value being assigned, so it can appear to work in testing.

Why not a compile error?

Assignment inside a condition is fully legal C — idioms like while ((c = getchar()) != EOF) from K&R depend on it. But both compilers know the pattern is usually a typo, and -Wall flags it:

warning: suggest parentheses around assignment used as truth value [-Wparentheses]

clang goes further and prints a fix-it hint: “use ‘==’ to turn this assignment into an equality comparison.” The convention for silencing the warning when the assignment is intentional is an extra pair of parentheses — if ((x = next())) — which tells both the compiler and the human “yes, I meant that.”

Why not undefined behavior?

Everything is sequenced and well-defined: assign, take the assignment’s value, branch on it. The program does the wrong thing reliably — which makes it a logic bug, not UB.

Defending Against the Typo

Three habits catch this bug before it ships:

  • Never ignore -Wparentheses — it’s in -Wall on both major compilers, and it fires precisely here. Building with -Werror turns the typo into a build failure.
  • Yoda conditions — writing if (0 == x) makes the typo’d version if (0 = x) a hard compile error, since you can’t assign to a constant. Some codebases mandate it; others find it hurts readability now that warnings do the job.
  • const what shouldn’t change — if a variable is never meant to be reassigned, declaring it const makes any accidental assignment a compile error everywhere, not just in conditions.

The equality operator itself — including why 3 == 3 yields the int 1 rather than a boolean — is covered in its own quiz question.

Frequently Asked Questions

What does if (x = 0) do in C?

It assigns 0 to x, then branches on the assigned value — 0, which is false. So the else branch always runs, and x is destroyed as a side effect. For a comparison, write x == 0.

Is assignment inside an if condition legal in C?

Yes — an assignment is an expression with a value, and idioms like while ((c = getchar()) != EOF) rely on that. Compilers warn under -Wall when an unparenthesized assignment is used as a condition, because it’s usually a typo; double parentheses signal intent.

Do Yoda conditions like if (0 == x) still make sense?

They convert the typo into a compile error, which is why older style guides required them. With -Wall -Werror catching the same bug automatically, most modern codebases prefer the natural order and let the compiler stand guard.

Related Reading

Recommended Books

This question is #68 in the C Programming Quiz App — 155 questions with explanations covering basics, operators, pointers, and more.
Download on Google Play →

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>