Operators
Operators are the built-in symbols that compute, compare, assign, and reach into objects: +, ==, =, [], *, and the rest. Precedence and associativity decide which one runs first when you do not write parentheses.
Detailed Explanation
You write total = price * count + tax. The compiler does not read that left to right as three equal steps. * binds tighter than +, and + binds tighter than =, so it is (total = ((price * count) + tax)). That ranking is operator precedence. When two operators share a precedence, associativity picks a side: a - b - c is (a - b) - c, not a - (b - c).
C++ inherits C's operators and then adds a few of its own (::, .*, ->*, <=> in C++20). Most of them can be overloaded for class types, which is why std::string + std::string concatenates and std::vector[i] indexes. Overloading does not change precedence. a + b * c still does the multiply first, even if + and * are your operators.
The groups you will use constantly:
Arithmetic. + - * / %. Integer division truncates toward zero. % is the remainder and follows the sign of the dividend in modern C++. Overflow of signed integers is undefined behaviour. Unsigned overflow wraps.
Comparison. == != < <= > >= and C++20's <=> (three-way comparison). Built-in == on pointers compares addresses, not the objects pointed at. Floating-point == is exact bit-pattern equality and is the wrong tool for measured values.
Logical and bitwise. && || ! short-circuit: if the left side of && is false, the right side is not evaluated. | & ^ ~ never short-circuit and work on bits. Mixing them with booleans compiles (true | false) and is almost always a mistake; use ||.
Assignment. = is not ==. Compound forms (+=, *=) exist for the arithmetic and bitwise operators. Assignment is right-associative: a = b = 0 sets both to 0.
Increment. ++x is pre-increment: increment, then yield the new value. x++ is post-increment: yield the old value, then increment. On a user-defined iterator, post-increment has to copy the iterator first, so range-for and generic code prefer ++it.
Member access. obj.m on an object, ptr->m on a pointer (defined as (*ptr).m). .* and ->* bind a pointer-to-member.
Other. [] subscript, () function call, , comma (evaluate left, yield right), ?: the only ternary, sizeof, alignof, new, delete, :: scope.
A second operand of &&, ||, ,, or ?: that is not evaluated cannot have side effects you depend on. if (p && p->ready()) is safe because p->ready() does not run when p is null. if (p & p->ready()) is not the same idea: & always evaluates both sides.
When an expression gets denser than a reader can rank from memory, use parentheses. Precedence exists so everyday arithmetic looks normal, not so you can write a one-liner that depends on the difference between << and +.
Code Examples
#include <iostream>
int main() {
int price = 20;
int count = 3;
int tax = 5;
int total = price * count + tax; // (20 * 3) + 5
std::cout << total << '\n'; // prints: 65
int a = 5;
std::cout << a++ << '\n'; // prints: 5, then a becomes 6
std::cout << ++a << '\n'; // a becomes 7, prints: 7
int* p = nullptr;
if (p && *p == 1) { // *p is not evaluated
std::cout << "reachable\n";
}
}#include <iostream>
#include <string>
int main() {
std::string id = "user";
id += "-" + std::to_string(42); // + is concatenation for string
std::cout << id << '\n'; // prints: user-42
int flags = 0b0101;
flags |= 0b0010; // set bit 1
std::cout << flags << '\n'; // prints: 7
}Common Gotchas
`=` inside `if` is assignment
`if (x = 0)` sets `x` to 0 and is false. Compilers warn. Write `if (x == 0)`.
Shift precedence is below arithmetic
`1 << 2 + 3` is `1 << 5`, not `(1 << 2) + 3`. Parenthesize shifts.
Overloaded `&&` and `||` lose short-circuit
if you define them for a class, both operands are always evaluated. Prefer an explicit method over overloading the logical operators.
Integer promotion happens before the operator runs
`unsigned short` operands promote to `int` on most platforms, so `a * b` can be signed. Use a wider explicit type when overflow matters.