Skip to content

Latest commit

 

History

History
53 lines (38 loc) · 2.12 KB

equality-operators-equal-equal-and-exclpt-equal.md

File metadata and controls

53 lines (38 loc) · 2.12 KB
title ms.date f1_keywords helpviewer_keywords ms.assetid
Equality Operators: == and !=
11/04/2016
not_eq
!=
==
!= operator
equality operator
not equal to comparison operator
equality operator [C++], syntax
== operator
not_eq operator
equal to operator
ba4e9659-2392-4fb4-be5a-910a2a6df45a

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 (1) if both operands have the same value; otherwise, it returns false (0). The not-equal-to operator (!=) returns true if the operands do not have the same value; otherwise, it returns false.

Operator Keyword for !=

The not_eq operator is the text equivalent of !=. There are two ways to access the not_eq operator in your programs: include the header file iso646.h, or compile with the /Za (Disable language extensions) compiler option.

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