The == operator checks equality. For example:
if ( a == b ) dosomething();
And in your example:
x = y == z;
x is true (1) if y is equal to z. If y is not equal to z, x is false (0).
A common mistake made by new C programmers (and a typo made by some very experienced ones):
if ( a = b ) dosomething();
In this case, b is assigned and then evaluated as a Boolean expression. Sometimes a programmer does this on purpose, but it is a bad form. Another programmer reading the code will not know if this was done intentionally (rarely) or unintentionally (much more likely). The best design:
if ( (a = b) == 0 ) // or != dosomething();
Here b is assigned a, then the result is compared with 0. The goal is clear. (Interestingly, I worked with C # programmers who never wrote pure C and could not tell you what it does.)
Billp3rd
source share