Short answer
The rule
Learn C++ says: "Integer division drops any fractions and returns an integer value." Its example is that 7 / 4 = 1 because the fractional portion of the result is dropped.
cppreference states the same thing more formally. If both operands have an integral type, the result is the algebraic quotient, and "the quotient is truncated towards zero (fractional part is discarded)". It is not rounded. 5 / 2 is 2.5, and it becomes 2.
How to keep the decimals
Learn C++ says that if either operand, or both, is a floating point value, the division operator performs floating point division and keeps the fraction. The lesson shows converting one side with static_cast<double>. This program follows that approach and was compiled and run with g++ 16.2 on Windows:
#include <iostream>
int main()
{
int x{ 5 };
int y{ 2 };
std::cout << "int / int = " << x / y << '\n';
std::cout << "double / int = " << static_cast<double>(x) / y << '\n';
std::cout << "5 % 2 = " << x % y << '\n';
int n{ -10 };
std::cout << n / 21 << ' ' << n % 21 << '\n';
return 0;
}
It printed:
int / int = 2
double / int = 2.5
5 % 2 = 1
0 -10
The first line is the surprise. The second line keeps the fraction because one side was converted to a double before the division.
A common mistake is to convert the result instead of an operand. Writing static_cast<double>(x / y) does the integer division first, gets 2, and only then converts 2 to 2.0. Convert one of the inputs. This mistake was not run here, it follows from the order the sources describe.
The remainder operator
The percent sign gives the remainder of an integer division. Learn C++'s operator table lists % as "The remainder of x divided by y". In the program above, 5 % 2 printed 1, because 2 goes into 5 twice with 1 left over.
cppreference gives a rule that ties the two operators together: if a / b can be represented, then (a / b) * b + a % b == a. Check it with 5 and 2: 2 times 2 plus 1 is 5.
Negative numbers
Because the quotient is truncated towards zero, the remainder follows from the identity above. cppreference's own example is -10 divided by 21: the quotient is 0 and the remainder is -10. The last line of the program printed exactly that. The page notes that before an older language issue was resolved, the sign of the remainder with negative operands was implementation-defined.
Never divide by zero
cppreference says that for both built-in division and remainder, "If rhs is zero, the behavior is undefined." Undefined behavior means the language promises nothing about what happens, so check a divisor before you use it.
Quick reference
| Expression | Result | Why |
|---|---|---|
5 / 2 | 2 | Both integers, fraction dropped |
5.0 / 2 | 2.5 | One side is floating point |
5 % 2 | 1 | Remainder of the division |
-10 / 21 | 0 | Truncated towards zero |
-10 % 21 | -10 | Fits the identity above |
If a wrong number in your own code turns out to be a variable you forgot to set, see what happens with an uninitialized variable. New to the structure around these lines? Read what int main() means in C++.
Teaches C++ to complete beginners as a game where you tap one real line of code at a time.
Get it on Google PlayFree to play. Halls 1 to 4 are free with no time limit, and free players can watch optional rewarded video ads for bonuses. Segfault Pro, a monthly subscription or a 3-month pass paid once, opens hall 5 and up, unlimited practice sets and mock exams, with no ads.
C++ Game: Segfault teaches C++ to complete beginners as a game. You never type. Each card shows a box and the number it needs, and you tap the one real line of C++ that makes it match. Halls 1 to 4 are free with no time limit, and it covers 11 halls from variables to if, loops, functions and arrays. It will not make you a programmer on its own, because you tap lines rather than write them.