Categories
c programming tips and tricks

Put the Constant As the First Term While Making Comparisons

Sometimes, we tend to confuse “=” operator with “==” operator. To avoid this, use the defensive programming approach. 0==x instead of x==0 so that 0=x can be caught by

It means you should write “1==x” instead of “x==1” so that the compiler will always flag an error for the miswritten “1=x”.

So whenever you mistakenly write the following.

if ( 1 = x )

The compiler will complain and refuse to compile the program.

While it’s not possible if you are comparing two variables. For example, the expression

if (x == y)

can be miss written as

if(x = y)

Leave a Reply

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