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 << '\n';
cout << "Floating division: " << floatDiv << '\n';
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 come 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 << '\n';
return 0;
}
x: 7, y: 5, z: 7
Operator Precedence (Practical Subset)
Operator precedence controls how expressions are grouped. Higher-precedence operators bind more tightly. Parentheses can always be used to force the desired grouping. (Associativity matters too: e.g., *, /, % are left-to-right; most unary operators are right-to-left.)
- Postfix: expr++, expr-- (very high precedence, evaluated after the expression)
- Prefix unary: +, -, ++, -- (on the operand itself)
- Multiplicative: *, /, %
- Additive: +, -
#include <iostream>
using namespace std;
int main() {
int a = 3, b = 2, c = 5;
int r1 = c + a * b; // 5 + (3*2) = 11
int r2 = (c + a) * b; // (5+3)*2 = 16
cout << r1 << ", " << r2 << '\n';
return 0;
}
11, 16
Type Promotions & Conversions (Why 5/2 differs from 5.0/2)
When operators mix types, C++ applies the usual arithmetic conversions (promotions) 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
Overflow & Division by Zero
Signed integer overflow is undefined behavior; unsigned overflow wraps modulo 2^N.
Integer division or modulus by zero is undefined behavior.
Floating-point overflow or division by zero typically yields infinities or NaNs (per IEEE-754) rather than UB, but still indicates an exceptional condition.