6.5.9 Equality operators

Previous Table of Contents "New C Standard" commentary

1212

equality-expression:
                relational-expression
                equality-expression == relational-expression
                equality-expression != relational-expression

1213 One of the following shall hold:

1214 —  both operands have arithmetic type;

1215 —  both operands are pointers to qualified or unqualified versions of compatible types;

1216 —  one operand is a pointer to an object or incomplete type and the other is a pointer to a qualified or unqualified version of void; or

1217 —  one operand is a pointer and the other is a null pointer constant.

1218 The == (equal to) and != (not equal to) operators are analogous to the relational operators except for their lower precedence.91)

1219 Each of the operators yields 1 if the specified relation is true and 0 if it is false.

1220 The result has type int.

1221 For any pair of operands, exactly one of the relations is true.

1222 If both of the operands have arithmetic type, the usual arithmetic conversions are performed.

1223 Values of complex types are equal if and only if both their real parts are equal and also their imaginary parts are equal.

1224 Any two values of arithmetic types from different type domains are equal if and only if the results of their conversions to the (complex) result type determined by the usual arithmetic conversions are equal.

1225 91) The expression a<b<c is not interpreted as in ordinary mathematics.

1226 As the syntax indicates, it means (a<b);

1227 in other words, “if a is less than b, compare 1 to c; otherwise, compare 0 to c”.

1228 90) Because of the precedences, a<b == c<d is 1 whenever a<b and c<d have the same truth-value.

1229 Otherwise, at least one operand is a pointer.

1230 If one operand is a pointer and the other is a null pointer constant, the null pointer constant is converted to the type of the pointer.

1231 If one operand is a pointer to an object or incomplete type and the other is a pointer to a qualified or unqualified version of void, the former is converted to the type of the latter.

1232 For the purposes of these operators, a pointer to an object that is not an element of an array behaves the same as a pointer to the first element of an array of length one with the type of the object as its element type.

1233 Two pointers compare equal if and only if both are null pointers, both are pointers to the same object (including a pointer to an object and a subobject at its beginning) or function, both are pointers to one past the last element of the same array object, or one is a pointer to one past the end of one array object and the other is a pointer to the start of a different array object that happens to immediately follow the first array object in the address space.92)

Next

Created at: 2008-01-30 02:39:43 The text from WG14/N1256 is copyright © ISO