HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to C++VariablesData TypesOperatorsStringsVectors (vector)Conditions (if / switch)Loops (for / while)FunctionsClasses (OOP)
REFERENCE
📖 Overview
🖥️ Output & Input
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array & Vector
🔁 Condition & Loop
🧩 Function & Class
C++›Operators

Operators

Math (+ - * / %), comparison (== < >), and logic (&& || !).

Simple enough for a 5-year-old to understand

Math

Example code
1
#include <iostream>
2
using namespace std;
3
int main() {
4
cout << 7 % 2 << endl;
Remainder: 1
CodeRemainder: 1
5
cout << 7 / 2 << endl;
Integer division: 3
CodeInteger division: 3
6
return 0;
7
}
Code
💡 Hint: There is no ** in C++; use #include <cmath> + pow(2, 10) for powers.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Print the remainder of the division of 23 by 4.

🎯 Expected output: 3

Loading editor…

About this topic

Most C++ operators come from C and behave the same way. The biggest difference C++ adds is operator overloading: you can define what addition or comparison operators mean for your own type.

The standard library uses this heavily. For string, the plus sign performs concatenation, for vector, square brackets provide element access, and stream operators are used to write output. None of these are built-in behaviors of the language, they are overloaded operators.

Traps coming from C were carried over as well. Writing a single equals sign inside a condition does not cause a compilation error, incrementing the same variable twice in an expression is undefined behavior, and bitwise operators are easily confused with logical operators.

What you will learn

  • Arithmetic and comparison operators
  • Short-circuit evaluation
  • What operator overloading is
  • How to prevent confusion between assignment and comparison

When to use?

Short-circuit evaluation is mandatory in pointer checks: the condition on the left checks if the pointer is valid, and the right side accesses its content. If the order is reversed, the program crashes.

Operator overloading feels natural for mathematical types — such as vectors, matrices, and fractions. Where the meaning is ambiguous, a named function is more readable.

Common mistakes

Wrong

#include <iostream>
using namespace std;

int main() {
    int value = 5;
    if (value = 10) {
        cout << "Ten" << endl;
    }
    return 0;
}

A single equals sign performs assignment and the value of the expression becomes 10; since 10 is not zero, the condition is always true and value gets corrupted as well. The compiler gives a warning but not an error. A double equals sign is used for comparison.

Correct

#include <iostream>
using namespace std;

int main() {
    int value = 5;
    if (value == 10) {
        cout << "Ten" << endl;
    }
    return 0;
}

Wrong

#include <iostream>
#include <string>
using namespace std;

int main() {
    string a = "Hel";
    cout << a + "lo" + '!' << endl;
    return 0;
}

In left-to-right evaluation, the first two concatenations produce a string, then it is added to a character, and this works. However, if you try to concatenate two char constants, addition performs integer arithmetic and prints a number to the screen. In C++, the meaning of the plus sign depends on the types of the operands.

Correct

#include <iostream>
#include <string>
using namespace std;

int main() {
    string a = "Hel";
    cout << a + "lo" + "!" << endl;
    return 0;
}

Wrong

#include <iostream>
using namespace std;

int main() {
    int i = 3;
    cout << i++ << " " << ++i << endl;
    return 0;
}

Since the same variable is modified twice in a single expression, the evaluation order is not guaranteed; prior to C++17, this is undefined behavior. Different compilers print different results. Splitting increment operations into separate lines is the only safe way.

Correct

#include <iostream>
using namespace std;

int main() {
    int i = 3;
    cout << i << " ";
    i += 2;
    cout << i << endl;
    return 0;
}

Frequently asked questions

When is operator overloading the right choice?

When the meaning of the operator is obvious to the reader. For a fraction type, it is clear that the plus sign means addition. On a customer object, what the plus sign does is ambiguous; a named function should be used there.

Is there a difference between the ternary operator and if?

The ternary operator is an expression, produces a value, and can be used on the right side of an assignment. if is a statement. When choosing between two values, the ternary operator is shorter and more readable; when nested, however, readability is quickly lost.

Should I memorize operator precedence?

Beyond arithmetic ordering, no. Use parentheses: there is no runtime cost and it makes the intent clear. The rule that bitwise operators come after comparison operators is the most surprising one, and parentheses are mandatory there.

Summary

A single equals sign in a condition does not cause a compilation error; it silently works incorrectly.

The meaning of the plus sign changes based on the types of the operands.

Do not modify the same variable twice in a single expression.

Operator overloading is used only when the meaning is obvious.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code