| Equality operators ('==' and '!=')

Previous | Binary operators | Next

The operators `'=='` and `'!='` are used to test for equality or inequality between arithmetic or pointer values, following rules similar to those for the relational operators. However, the equality operators have lower precedence than the relational operators, and you can also compare certain pointer types not allowed with relational operations.
In the expressions expr1 == expr2 and expr1 != expr2, the operands must conform to one of the following sets of conditions:

1. Both expr1 and expr2 are of arithmetic type.
2. Both expr1 and expr2 are pointers to qualified or unqualified versions of compatible types.
3. One of expr1 and expr2 is a pointer to an object, and the other is a pointer to a qualified or unqualified version of void. In this case, the pointer to an object is converted to the type of the other operand (a void pointer).
4. One of expr1 or expr2 is a pointer and the other is a null pointer constant.

If expr1 and expr2 have types that are valid operand types for a relational operator, the same comparison rules as for the relational operators apply.
If expr1 and expr2 are pointers to function types, expr1 == expr2 gives 1 (true) if they are both null or if they both point to the same function. Conversely, if expr1 == expr2 gives 1 (true), then either expr1 and expr2 point to the same function, or they are both null.
The expression expr1 != expr2 follows the same rules, except that the result is 1 (true) if the operands are unequal, and 0 (false) if the operands are equal.
Floating point comparisons are internally executed using the fcmp function. See the description of this function for more info about rules of comparisons for floating point values.