C++ Operators Reference
Operators run all the real work in a program. This is the cheat-sheet lineup, grouped by what each does.
Arithmetic operators
| Operator | Does what | Example |
|---|
+ | addition | a + b |
- | subtraction | a - b |
* | multiplication | a * b |
/ | division | a / b |
% | remainder (modulo) | a % b |
Integer division drops the fraction: 7 / 2 is 3, not 3.5.
Comparison operators
| Operator | Meaning | Value |
|---|
== | equal to | true/false |
!= | not equal to | true/false |
> | greater than | true/false |
< | less than | true/false |
>= | greater than or equal | true/false |
<= | less than or equal | true/false |
Logical operators
| Operator | Pronunciation | Meaning |
|---|
&& | and | true only if both sides are true |
| ` | | ` | or | true if at least one side is true |
! | not | flips true to false and false to true |
Assignment operators
| Operator | Shorthand for |
|---|
= | sets a value directly |
+= | a = a + b |
-= | a = a - b |
*= | a = a * b |
/= | a = a / b |
%= | a = a % b |
&= | bitwise and, then assign |
| ` | =` | bitwise or, then assign |
^= | bitwise xor, then assign |
<<= | left-shift bits, then assign |
>>= | right-shift bits, then assign |
Increment / decrement
| Operator | Meaning |
|---|
++x | add 1, then use the new value |
x++ | use the current value, then add 1 |
--x | subtract 1, then use the new value |
x-- | use the current value, then subtract 1 |
Bitwise operators
| Operator | Meaning |
|---|
& | bitwise AND |
| ` | ` | bitwise OR |
^ | bitwise XOR |
~ | bitwise NOT |
<< | shift bits left (also the stream insertion operator) |
>> | shift bits right (also the stream extraction operator) |
Ternary (the only 3-part operator)
| Operator | Pattern |
|---|
? : | condition ? valueIfTrue : valueIfFalse |
int max = (a > b) ? a : b; picks a or b based on the condition. One line, no if statement.
Scope and pointer operators
| Operator | What it does |
|---|
:: | scope resolution - reach a name in a class or namespace (std::cout) |
. | member access - access a field or method of an object (player.score) |
-> | arrow - access a member through a pointer (ptr->score) |
* | pointer dereference - get the value the pointer points at |
& | address-of - get the memory address of a variable |
Other common operators
| Operator | Meaning |
|---|
sizeof | byte size of a type or variable |
, | comma - run several expressions, the last one is the result |
<< and >> | stream out and stream in (overloaded for cout/cin) |
& (in a class) | references - alias to another variable (int& r = x) |
:: | scope resolution - same as noted above |
Precedence shortcut
Remember the loose ordering (highest to lowest work, roughly):
() \[\] -> . - things, subscripts, members.! ~ ++ -- - * & - unary prefixes.* / % - multiply and divide.+ - - add and subtract.<< >> - stream/shift.< > <= >= - comparisons.== != - equality.&& - logical and.|| - logical or.= += -= ... - assignment (right-to-left)., - lowest of all.
If you're ever unsure, add parentheses. Clear beats clever.
Notes
/ on integers rounds toward zero - the fraction disappears.% is meaningless for floats; use fmod from <cmath`` if needed.== compares values; = assigns a value. Swapping them is a classic bug.&& and || stop early (short-circuit): if the left side decides it, the right side never runs.- The
<< symbol does double duty: bit shifting in math and "send to stream" for output.
TL;DR
- Arithmetic:
+ - * / %. - Comparison:
== != > < >= <=. - Logical:
&& || !. - Assignment shortcuts:
+= -= *= /= %=. - Ternary
? : picks one of two values in a single line. :: resolves scope, -> reaches through pointers, & takes an address.- When unsure of precedence, use parentheses.