Equality operators: == and !=
Syntax
expression
==
expression
expression!=
expression
Remarks
The binary equality operators compare their operands for strict equality or inequality.
The equality operators, equal to (==
) and not equal to (!=
), have lower precedence than the relational operators, but they behave similarly. The result type for these operators is bool
.
The equal-to operator (==
) returns true
if both operands have the same value; otherwise, it returns false
. The not-equal-to operator (!=
) returns true
if the operands don't have the same value; otherwise, it returns false
.
Operator keyword for !=
C++ specifies not_eq
as an alternative spelling for !=
. (There's no alternative spelling for ==
.) In C, the alternative spelling is provided as a macro in the <iso646.h> header. In C++, the alternative spelling is a keyword; use of <iso646.h> or the C++ equivalent <ciso646> is deprecated. In Microsoft C++, the /permissive-
or /Za
compiler option is required to enable the alternative spelling.
Example
// expre_Equality_Operators.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;
int main() {
cout << boolalpha
<< "The true expression 3 != 2 yields: "
<< (3 != 2) << endl
<< "The false expression 20 == 10 yields: "
<< (20 == 10) << endl;
}
Equality operators can compare pointers to members of the same type. In such a comparison, pointer-to-member conversions are performed. Pointers to members can also be compared to a constant expression that evaluates to 0.
See also
Expressions with binary operators
C++ built-in operators, precedence; and associativity
C relational and equality operators
Feedback
Submit and view feedback for