Arithmetic operators are the most fundamental tools in C++, allowing a program to perform mathematical calculations. In C++, these operators are symbols that trigger specific mathematical actions on one or more operands.
Theoretically, these operators are divided into two categories based on the number of operands they require: Unary (one operand) and Binary (two operands).
The behavior of an arithmetic operator depends heavily on the data type of the operands involved. This is a critical theoretical concept in C++:
Integer Arithmetic: If both operands are integers, the result is an integer. This is particularly important for division (e.g., $10 / 3$ results in $3$, not $3.33$).
Floating-Point Arithmetic: If at least one operand is a float or double, the result is a floating-point number.
The Assignment Rule: The result of an arithmetic expression is calculated first, and then it is assigned to the variable on the left of the = operator.
Binary operators require two operands to perform a calculation.
| Operator | Operation | Description |
| + | Addition | Adds two operands together. |
| - | Subtraction | Subtracts the second operand from the first. |
| * | Multiplication | Multiplies both operands. |
| / | Division | Divides the numerator by the denominator. |
| % | Modulo | Returns the remainder of an integer division. |
The modulo operator is unique because it only works with integers. It is used to find the remainder. For example, $13 \% 5$ is $3$ because 5 goes into 13 twice, with 3 left over.
Theory: Modulo is frequently used to check for even/odd numbers ($num \% 2 == 0$) or to keep a value within a certain range (circular arrays).
Unary operators act upon a single operand to modify its value. These are the most commonly used operators in loops.
Increases the value of the operand by 1.
Prefix (++x): Increments the value first, then uses it in the expression.
Postfix (x++): Uses the current value in the expression first, then increments it.
Decreases the value of the operand by 1.
Prefix (--x): Decrements then uses.
Postfix (x--): Uses then decrements.
When multiple operators appear in a single expression (e.g., $x = 5 + 3 * 2$), C++ follows strict mathematical rules to determine which happens first.
Parentheses (): Highest priority. Always calculated first.
Unary Operators ++, --: Second priority.
Multiplicative *, /, %: Third priority.
Additive +, -: Lowest priority among arithmetic operators.
Associativity: If two operators have the same precedence (like * and /), they are calculated from Left to Right.
#include <iostream> using namespace std; int main() { int a = 10, b = 3; float x = 10.0, y = 3.0; cout << "--- Binary Operators ---" << endl; cout << "Addition (10 + 3): " << a + b << endl; cout << "Integer Division (10 / 3): " << a / b << endl; // Truncates decimal cout << "Float Division (10.0 / 3.0): " << x / y << endl; cout << "Modulo (10 % 3): " << a % b << endl; // Remainder is 1 cout << "\n--- Unary Operators ---" << endl; int c = 5; cout << "Original C: " << c << endl; cout << "Postfix C++: " << c++ << " (Shows 5, but C is now 6)" << endl; cout << "Prefix ++C: " << ++c << " (Increments 6 to 7, then shows 7)" << endl; // Complex Expression: 10 + 3 * 2 // Multiply first: 3 * 2 = 6. Then add: 10 + 6 = 16. int result = a + b * 2; cout << "\nResult of 10 + 3 * 2: " << result << endl; return 0; }
| Operator Type | Symbol | Precedence | Associativity |
| Primary | () | 1 | Left to Right |
| Unary | ++, -- | 2 | Right to Left |
| Multiplicative | *, /, % | 3 | Left to Right |
| Additive | +, - | 4 | Left to Right |
Copyright ©2025. All Rights Reserved Emblab THE RAVE INNOVATION