C++ Arithmetic Operators
Introduction to Arithmetic Operators
Arithmetic operators in C++ are used to perform basic mathematical calculations on numeric values, such as variables and constants. These operators include addition, subtraction, multiplication, division, and modulus, and are essential for expressing computations in programs.
Basic Arithmetic Operators
Operator | Name | Example | Result |
---|---|---|---|
+ | Addition | 5 + 2 | 7 |
- | Subtraction | 5 - 2 | 3 |
* | Multiplication | 5 * 2 | 10 |
/ | Division | 5 / 2 | 2 (integer division; truncates toward zero) |
% | Modulus (Remainder) | 5 % 2 | 1 |
Division Examples
#include <iostream>
using namespace std;
int main() {
int intDiv = 5 / 2; // 2
double floatDiv = 5.0 / 2; // 2.5
cout << "Integer division: " << intDiv << endl;
cout << "Floating division: " << floatDiv << endl;
return 0;
}
Integer division: 2 Floating division: 2.5
Increment and Decrement Operators
C++ provides the increment ("++") and decrement ("--") operators to adjust the value of a variable by 1. These can be used in prefix and postfix forms, which differ in whether the change happens before or after the value is used in an expression.
#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = x++; // Post-increment: y gets 5, then x becomes 6
int z = ++x; // Pre-increment: x becomes 7, then z gets 7
cout << "x: " << x << ", y: " << y << ", z: " << z << endl;
return 0;
}
x: 7, y: 5, z: 7
Operator Precedence
Operator precedence determines the order in which different operators are evaluated in an expression. Operators with higher precedence are executed first. Parentheses can always be used to explicitly control the order of evaluation. (Associativity matters too: *, /, % are left-to-right; most unary operators are right-to-left.)
- Parentheses () (highest precedence)
- Postfix: expr++, expr-- (bind very tightly)
- Prefix unary: +, -, ++, -- (apply to the operand itself)
- Multiplication, Division, Modulus: *, /, %
- Addition, Subtraction: +, - (among the lowest here)
#include <iostream>
using namespace std;
int main() {
int result = 5 + 3 * 2; // 5 + (3 * 2) = 11
cout << result << endl;
return 0;
}
11
Type Promotions & Conversions
When operands differ in type, C++ applies the usual arithmetic conversions to a common type. Two ints divide as integers (truncating). If either operand is floating-point, both are converted to a floating type and floating-point division is performed.
#include <iostream>
using namespace std;
int main() {
cout << (5 / 2) << '\n'; // 2 (int / int)
cout << (5.0 / 2) << '\n'; // 2.5(double / int -> double)
cout << (5 / 2.0f) << '\n'; // 2.5(int / float -> float)
return 0;
}
2 2.5 2.5
Edge Cases & Safety
Signed integer overflow is undefined behavior; unsigned overflow wraps modulo 2^N.
Integer division or modulus by zero is undefined behavior.
Floating-point division by zero or overflow yields infinities or NaNs (IEEE-754) instead of UB, but still indicates an exceptional condition.