Equality operators: == and != (2024)

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

Equality operators: == and != (2024)
Top Articles
Latest Posts
Article information

Author: Frankie Dare

Last Updated:

Views: 5600

Rating: 4.2 / 5 (73 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Frankie Dare

Birthday: 2000-01-27

Address: Suite 313 45115 Caridad Freeway, Port Barabaraville, MS 66713

Phone: +3769542039359

Job: Sales Manager

Hobby: Baton twirling, Stand-up comedy, Leather crafting, Rugby, tabletop games, Jigsaw puzzles, Air sports

Introduction: My name is Frankie Dare, I am a funny, beautiful, proud, fair, pleasant, cheerful, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.