Skip to content
Tutorials Center
My learning

Topics

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.

C
int q = 17 / 5;      /* 3 */
int r = 17 % 5;      /* 2 */
double d = 17 / 5.0; /* 3.4 */

Try it

C Integer and floating arithmetic

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.

C Negative operands and the remainder

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

C
+ 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, so a - b - c is (a - b) - c. See operator precedence.
  • Integer division truncates toward zero (C99 and later), so -7 / 2 is -3. The identity (a / b) * b + a % b == a always holds.
  • The % result takes the sign of the dividend. -7 % 3 is -1, not 2. For a non-negative modulo, add the modulus and reduce again, as mod_positive does.
  • % does not work on floating-point types. Use fmod from <math.h>.
  • Mixed types convert first. int / double is a double division. 5 / 2 * 1.0 is still 2.0 because 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 char and short operands become int before arithmetic, so a + b for two unsigned char values can exceed 255.
  • Beware of overflow in intermediate results such as a * b / c; use a wider type or check with ckd_mul from <stdckdint.h>.
  • Compound forms (+=, /=, ...) are in assignment operators.