what is the meaning of == sign?

The == operator tests for 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 novice C programmers (and a typo made by some very experienced ones as well) is:

if ( a = b )
    dosomething();

In this case, b is assigned to a then evaluated as a boolean expression. Sometimes a programmer will do this deliberately but it’s bad form. Another programmer reading the code won’t know if it was done intentionally (rarely) or inadvertently (much more likely). A better construct would be:

if ( (a = b) == 0 )   // or !=
    dosomething();

Here, b is assigned to a, then the result is compared with 0. The intent is clear. (Interestingly, I’ve worked with C# programmers who have never written pure C and couldn’t tell you what this does.)

Leave a Comment