Why use if (0 == Indx) instead of (Indx == 0) - is there a difference?

I have a basic question that annoys me a lot and I canโ€™t understand why the programmer uses it.

if (0 == Indx) { //do something } 

What the above code does and how it differs from the one below.

 if (Indx == 0) { // do something } 

I am trying to understand some source code written for embedded systems.

+5
source share
1 answer

Some programmers prefer to use this:

 if (0 == Indx) 

because this line

 if (Indx == 0) 

can be "easily" coded by mistake, like an assignment operator (instead of a comparison)

 if (Indx = 0) //assignment, not comparison. 

And this is quite true in C.

Indx = 0 is an expression that returns 0 (which also assigns 0 Indx).

As mentioned in the comments on this answer, most modern compilers will show you warnings if you have such a task inside if if.

You can learn more about the advantages and disadvantages here .

+8
source

Source: https://habr.com/ru/post/1259426/


All Articles