C Reference ยท Operators and Expressions
Arithmetic operators in C
Reference for C arithmetic operators +, -, *, / and %: operand types, integer versus floating division, the sign of the remainder, unary plus and minus, and overflow.
C has five binary arithmetic operators, +, -, *, / and %, plus the unary + and -. They work on integer and floating-point operands, except %, which accepts integers only. Before the operation the operands go through the usual arithmetic conversions, so the type of the result depends on the types of both operands, not on where you store it.
The most common mistake is expecting / to produce a fraction when both operands are integers. It does integer division and discards the fractional part.
int q = 17 / 5; /* 3 */ int r = 17 % 5; /* 2 */ double d = 17 / 5.0; /* 3.4 */
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
+ expr - expr a * b a / b a % b a + b a - b
| Operator | Operands | Result |
|---|---|---|
unary +, unary - |
One arithmetic operand | The promoted value, or its negation. |
* |
Two arithmetic operands | Product. |
/ |
Two arithmetic operands | Quotient. Integer operands: truncated toward zero. |
% |
Two integer operands | Remainder with the sign of the left operand. |
+, - |
Two arithmetic operands, or a pointer and an integer (see pointer arithmetic), or two pointers for - |
Sum or difference. |
Errors / undefined behavior: integer division or remainder by zero is undefined behavior. INT_MIN / -1 and INT_MIN % -1 are undefined because the quotient does not fit. Signed overflow in +, -, * and unary - is undefined. Unsigned arithmetic wraps modulo 2^N and is always defined.
Usage notes
- Precedence: unary operators bind tightest, then
* / %, then+ -. Same-level binary operators group left to right, soa - b - cis(a - b) - c. See operator precedence. - Integer division truncates toward zero (C99 and later), so
-7 / 2is-3. The identity(a / b) * b + a % b == aalways holds. - The
%result takes the sign of the dividend.-7 % 3is-1, not2. For a non-negative modulo, add the modulus and reduce again, asmod_positivedoes. %does not work on floating-point types. Usefmodfrom<math.h>.- Mixed types convert first.
int / doubleis adoubledivision.5 / 2 * 1.0is still2.0because the integer division happens first. - Floating division by zero produces infinity or NaN on IEEE 754 platforms and does not trap by default, though the C standard calls it undefined without Annex F support.
- Small types promote:
unsigned charandshortoperands becomeintbefore arithmetic, soa + bfor twounsigned charvalues can exceed 255. - Beware of overflow in intermediate results such as
a * b / c; use a wider type or check withckd_mulfrom<stdckdint.h>. - Compound forms (
+=,/=, ...) are in assignment operators.