What Operators Do
An operator is a symbol that performs an action on one or more values, called operands. You've already been using one: the = that binds a value to a variable. C++ ships with a rich set of operators for math, comparison, logic, and bit manipulation - and a few sharp edges that trip up newcomers.
In the previous page you saw how const locks a value down. Operators are how you compute the values you store, whether they're constant or not. Let's walk through the families you'll use every day.
Arithmetic Operators
The five arithmetic operators are +, -, *, /, and % (modulo, the remainder of a division):
The big gotcha lives in that quotient. 17 / 5 prints 3, not 3.4. When both operands are integers, / does integer division and throws away the fractional part - it truncates toward zero, it does not round. If you want a real fraction, at least one operand must be floating-point:
The % operator only works on integers. Reaching for % on a double is a compile error - use std::fmod from <cmath> for floating-point remainders.
Assignment and Compound Operators
A single = assigns; it does not compare. C++ gives you compound forms that combine an operation with assignment so you don't repeat the variable name:
A classic, painful mistake is writing = where you meant == inside a condition. if (x = 0) assigns 0 to x and then tests the result (which is falsy), instead of comparing. Modern compilers warn about this when you turn warnings on - keep -Wall enabled and treat the warning seriously.
Comparison Operators
Comparison operators ask a yes/no question and produce a bool (true or false):
By default cout prints a bool as 1 or 0. Stream boolalpha once and it switches to the words true/false for the rest of that stream.
A subtle trap: don't chain comparisons like 1 < x < 10. That parses as (1 < x) < 10 - the first comparison yields a bool (0 or 1), which is then compared to 10, so the whole thing is almost always true. Write 1 < x && x < 10 instead.
Logical Operators and Short-Circuiting
&& (and), || (or), and ! (not) combine boolean expressions. The first two short-circuit: evaluation stops as soon as the result is known.
Neither check("A") nor check("B") ever runs - that's short-circuiting. It's not just an optimization; it's a tool. You can safely write if (ptr != nullptr && ptr->ready) because the ptr->ready part is only reached when ptr is non-null, avoiding a dangling-pointer dereference.
Increment and Decrement
++ adds one; -- subtracts one. Each has a prefix and a postfix form, and the difference matters when you use the result:
When you only want the side effect (in a for loop, say), prefer ++i. For plain int it's identical, but for heavier types like iterators, post-increment has to copy the old value first, which is wasted work.
One more warning: do not modify the same variable twice in a single expression, like i = i++ + 1; or arr[i] = i++;. The order of those updates is unspecified and the result is undefined behavior. Keep each variable to one modification per statement.
Bitwise Operators and Precedence
For low-level work there are bitwise operators: & (and), | (or), ^ (xor), ~ (not), and the shifts << and >>.
Watch out: << and >> are also the stream operators on cout. Inside a cout line you usually need parentheses around a bit shift, otherwise the compiler reads it as a stream insertion.
Finally, precedence decides what binds first when you mix operators. * and / bind tighter than + and -, just like in math, so 2 + 3 * 4 is 14. Comparison binds looser than arithmetic, and logical &&/|| looser still. When in doubt, don't memorize the full table - add parentheses. (a + b) * c is clearer than relying on the reader to recall the rules.
Next: Type Casting
You saw above that (double)a / b forced an integer into a floating-point division. That's a cast - deliberately converting a value from one type to another. In the next page we'll cover C++'s conversion tools, from implicit promotions to static_cast, and when each is safe.
Frequently Asked Questions
What are the operators in C++?
C++ groups operators into arithmetic (+ - * / %), comparison (== != < > <= >=), logical (&& || !), assignment (= += -= ...), increment/decrement (++ --), and bitwise (& | ^ ~ << >>). There's also the ternary ?: and a handful of others like sizeof.
Why does 5 / 2 equal 2 in C++?
Because both operands are int, so / performs integer division and the fractional part is discarded - not rounded. To get 2.5, make at least one operand a floating-point value: 5.0 / 2 or 5 / 2.0.
What is the difference between ++i and i++ in C++?
Both add 1 to i. ++i (pre-increment) increments first and yields the new value; i++ (post-increment) yields the old value, then increments. When you only care about the side effect, prefer ++i.