Why This Simple C Program Always Returns TRUE! (Common Beginner Mistake)

Ever wondered what happens when you accidentally use the assignment operator (=) inside an if condition in C program instead of the equality operator (==)?
This quick C tutorial demonstrates how a tiny typo can completely change the behavior of your code.

In this C interview question you’ll learn the following:

  • The behaviour of if condition in C
  • How assignment inside conditions actually works in C
  • The difference between = and ==
  • How this bug can silently break real programs
  • Best practices to avoid this common mistake

The if C program question

Take a look at the program and tell what do you think will happen when executed:

#include <stdio.h>

int main(){
    int x = 5;

    if ( x = 10)
        printf("TRUE, x is: %d\n", x);
    else
        printf("FALSE, x is: %d\n", x);

    return 0;
}

Here are your options:

A. TRUE, x is 5
B. TRUE, x is 10
C. FALSE, x is 5
C. FALSE, x is 10

Write the correct option in the comment box and then copy the code and execute in our online C compiler to see what output it gives.

The answer is B. TRUE, x is 10. Let me explain you why does this happen.

TL;DR

Look at this shorts video which explains the reason of this behavior.

Explanation of behavior of IF conditional statement in C

When you add an if condition, it always look for true or false (1 or 0 respectively) and nothing else.

When you add this statement if ( x = 10), what do you think what is happening here?

x = 10 is an assignment operation and it will be succeeded. When it is succeeded, the return of the status will be 1 or true.

So, this if ( x = 10 ) will always be true.

So, what’s the solutions?

We write = sign to show one thing is equal to other.

But in C, this is called as an assignment operator and it assigns the value in right to the variable declared in left.

For example:

a = 10,
b = 100,
c = a;
and so on…

If we have to compare two numbers, or variables, we have to use the equal to == operator.

So, the correct way of using it is this:
if (x == 10)

Good practice to follow

@mikhailkanygin8817 on Youtube replied as below:

Always place constant first inside if condition check statement. if (10 == x) { … Using this rule will always prevent you from such a mistakes. In that way If you misuse compare operator that would cause be a compilation exception instead of placing a bug.

This will fix this kind of issue and there is one problem in writing this way.

Developers in big production code base mostly do not use this style of checking. Do you know why?

It’s because of readability. We think and speak: “If x equals 10…” and not “If 10 equals x…”.

This difficulty in readability may not be evident from a small program but when you do code walk through on larger codebase, you yourself find difficult reading and understanding if something is written like this.

The whole idea of high level language like C programming is based on the way we humans understand and then write code for machines.

It is better to learn this small difference and make a habit to use the == which is the right way to compare two numbers in C programming.

Conclusion

This looks to be a simple if conditional check interview question in C but this small mistake may break everything in your C program.

This may happen when you just start learning C programming but as you learn C on a daily basis you will naturally start using == when you are comparing two numbers or variables.

If you have any doubts on concepts of if else or other conditional statements in C, then you can ask it on the forum page.

Make use of our online C compiler tool which is pretty easy to work when you are learning online.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.