C Reference ยท Operators and Expressions
Integer division and modulo in C
Reference for C integer division and the remainder operator: truncation toward zero, sign of the remainder, division by zero, INT_MIN / -1 and floor division.
When both operands of / are integers, C performs integer division and discards the fractional part. Since C99 the quotient is truncated toward zero: 7 / 2 is 3 and -7 / 2 is -3. The remainder operator % is defined to match, so that (a / b) * b + a % b == a always holds when a / b is representable.
This differs from the "floor" division and always-positive modulo of languages such as Python, and it is the source of many off-by-one and negative-index bugs. Both operators are undefined when the divisor is zero.
int q = 17 / 5; /* 3 */ int r = 17 % 5; /* 2 */ double d = 17 / 5; /* 3.0: the division happened in integers first */
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
dividend / divisor /* quotient */ dividend % divisor /* remainder, integers only */
| Name | Description |
|---|---|
dividend |
The value being divided. Both operands go through the usual arithmetic conversions first. |
divisor |
Must not be zero. |
Result: if both operands are integers, / gives the algebraic quotient truncated toward zero. % gives dividend - (dividend / divisor) * divisor, so its sign follows the dividend. If either operand of / is floating, floating-point division is done. % requires integer operands.
Errors / undefined behavior:
- Division or remainder by zero is undefined behavior for integers (typically a crash with SIGFPE on x86). For floating point,
x / 0.0gives infinity or NaN under IEEE 754. INT_MIN / -1andINT_MIN % -1are undefined because the quotient cannot be represented; on x86 they also trap.
Usage notes
- Truncation toward zero is guaranteed since C99. In C89 it was implementation-defined for negative operands, so old code may assume otherwise.
- The remainder takes the sign of the dividend.
-7 % 3is -1, not 2. Use a helper (as above) for index wrap-around, hashing and angle normalization. - Convert before dividing.
(double)a / bora / 2.0do floating division;(double)(a / b)converts after the integer division has already thrown the fraction away. - Unsigned operands convert signed ones:
-7 / 2uconverts -7 to a huge unsigned value. Avoid mixing them. - Powers of two.
x / 2equalsx >> 1only for non-negativex. Let the compiler optimize/ 2; it handles the sign for you. - Zero checks. Test the divisor before dividing, or guard with
b != 0 && a / b > 1. A divisor read from input must always be checked. - Floating remainder.
fmod(x, y)from<math.h>takes the sign ofxas well.remainder()rounds the quotient to the nearest integer instead of truncating. div()andldiv()in<stdlib.h>return quotient and remainder in a struct, from one operation, with the same truncation rule.- Compile-time constants: the compiler warns about a literal division by zero, but it is still undefined behavior.