Comparison Operators

Comparison Operators in C++

Learn C++ comparison operators: ==, !=, <, >, <= and >=, how they return bool, common mistakes, and how to compare strings and floating-point numbers safely.

On this page
  1. What comparison operators do
  2. The six comparison operators
  3. Comparisons produce a bool
  4. Using comparisons in decisions
  5. Comparing strings
  6. Comparing floating-point numbers
  7. Common mistakes
  8. Comparison operators in C
  9. The three-way comparison operator
  10. Summary

What comparison operators do

Comparison operators compare two values and report how they relate. Each comparison gives a true or false answer, and that answer is what lets a program make decisions. You’ll use them in if statements, in loops, and anywhere a program needs to check a value.

The six comparison operators

Operator Meaning True when
== equal to both sides hold the same value
!= not equal to the sides hold different values
< less than the left side is smaller
> greater than the left side is larger
<= less than or equal to the left side is smaller or the same
>= greater than or equal to the left side is larger or the same

Here they are in a small program:

#include <iostream>

int main() {
    int a = 7;
    int b = 3;

    std::cout << std::boolalpha;
    std::cout << (a == b) << '\n';  // false
    std::cout << (a != b) << '\n';  // true
    std::cout << (a > b) << '\n';   // true
    std::cout << (a <= b) << '\n';  // false
}

The parentheses are required. The << operator binds more tightly than these comparisons, so without them the line won’t compile.

Comparisons produce a bool

The result of every comparison has type bool, which holds either true or false. You can store that result in a variable:

int age = 21;
bool is_adult = age >= 18;

The bool type is one of the primitive data types in c, and it holds only those two values. By default, std::cout prints a bool as 1 or 0. Add std::boolalpha to print the words true and false, as the example above does.

Using comparisons in decisions

Comparisons drive if statements and loops:

#include <iostream>

int main() {
    int score = 72;

    if (score >= 90) {
        std::cout << "Grade A\n";
    } else if (score >= 60) {
        std::cout << "Pass\n";
    } else {
        std::cout << "Try again\n";
    }

    for (int i = 0; i < 3; ++i) {
        std::cout << "Attempt " << i + 1 << '\n';
    }
}

The first block prints Pass, because 72 is not at least 90 but is at least 60. The loop runs while i < 3 stays true, so it prints three attempts. A while loop, covered in c while loop syntax, works the same way: it repeats for as long as its comparison is true.

If the if-else examples are new to you, the guide to if else statement syntax in c walks through the details. When you compare one variable against many fixed values, a switch case statement in c can be easier to read than a long chain of == checks.

Comparing strings

std::string supports all six operators. It compares text in dictionary order, one character at a time:

#include <iostream>
#include <string>

int main() {
    std::string first = "apple";
    std::string second = "banana";

    std::cout << std::boolalpha;
    std::cout << (first == second) << '\n';  // false
    std::cout << (first < second) << '\n';   // true, because 'a' comes before 'b'
}

C-style strings, which are arrays of char, behave differently. With ==, two char arrays are compared by address, so two arrays holding the same text are not equal. To compare their contents, use std::strcmp from <cstring>, which returns 0 when the strings match. For a step-by-step walkthrough, see how to compare two strings in c.

Comparing floating-point numbers

Many decimal fractions, such as 0.1, can’t be stored exactly in binary floating-point. An equality check can therefore fail when you expect it to pass:

#include <iostream>

int main() {
    double sum = 0.1 + 0.2;
    std::cout << std::boolalpha;
    std::cout << (sum == 0.3) << '\n';  // false
}

Instead, check whether the two values are close enough. Compare the difference against a small tolerance that suits your problem, such as std::fabs(sum - 0.3) < 1e-9, where std::fabs comes from <cmath>.

Common mistakes

Using = where you meant ==. The condition if (x = 5) assigns 5 to x. Because 5 is not zero, the condition is true. Compilers can warn about this, so turn on warnings. The single = belongs to c assignment operators, and mixing it up with == is a common beginner error.

Chaining comparisons like algebra. In math, 1 < x < 5 means x is between 1 and 5. In C++, the expression is grouped from the left, so it becomes (1 < x) < 5. The inner comparison gives 0 or 1, and both are less than 5, so the whole expression is true for every value of x. Write x > 1 && x < 5 instead.

Comparing signed and unsigned integers.

int a = -1;
unsigned int b = 1;
std::cout << (a < b) << '\n';  // prints 0

Before comparing, C++ converts the signed value to unsigned, so -1 becomes a very large number. Compilers can warn about this mix. The reason is an implicit conversion, and if signed vs unsigned in c still feels unclear, review it first.

Comparison operators in C

C uses the same six operators with the same meanings. The main difference is the result type. A comparison in C produces an int, with 1 for true and 0 for false. Including <stdbool.h> adds the names bool, true and false for storing values, but a comparison still produces an int.

The three-way comparison operator

Since C++20, the <=> operator, often called the spaceship operator, compares two values in one expression and reports whether the left side is less than, equal to, or greater than the right side. It returns an ordering result rather than a plain bool. Most code can keep using the six operators above, but <=> is worth recognizing when you read modern C++.

Summary

  • The six comparison operators are ==, !=, <, >, <= and >=.
  • Each comparison produces a bool, true or false.
  • Use == to compare and = to assign.
  • Compare floating-point numbers with a tolerance, not exact equality.
  • std::string compares by value, but char arrays need std::strcmp.

Keep exploring

Guides from other topics

All topics →

std::array in C++

A complete guide to std::array in C++: declaring, initializing, reading elements, looping, passing to functions, and how it compares to C-style arrays.

std::array 5 min read