Arithmetic operators perform math. / between two integers is integer division (the fraction is dropped), and % gives the remainder. % works only on integer types; use fmod from <math.h> for doubles.
Remember
a + b a - b a * b
a / b /* int / int -> int, truncates toward zero */
a % b /* remainder, sign follows a (C99) */
-a /* unary minus */
Reference
Cheat Sheet
Keep these facts handy while you work with Arithmetic Operators in C.
Topic
Detail
Division
17 / 5 is 3; 17 / 5.0 is 3.4
Remainder
17 % 5 is 2
Divide by zero
Undefined behavior for integers
Float remainder
fmod(x, y) in <math.h>
Code
C Examples
Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Relational Operators.
Example 1 — All five operators
Integer and floating-point division give different answers.
C
#include <stdio.h>
int main(void) {
int a = 17, b = 5;
printf("a + b = %d\n", a + b);
printf("a - b = %d\n", a - b);
printf("a * b = %d\n", a * b);
printf("a / b = %d\n", a / b);
printf("a %% b = %d\n", a % b);
printf("a / 5.0 = %.1f\n", a / 5.0);
return 0;
}
Output
a + b = 22
a - b = 12
a * b = 85
a / b = 3
a % b = 2
a / 5.0 = 3.4
How It Works
a / b drops the fraction because both are int.
%% in the format string prints a literal %.
Using 5.0 makes the division floating point.
Edge Cases & Pitfalls
Check these before relying on Arithmetic Operators in real programs.
div0
Division by zero
x / 0 and x % 0 are undefined for integers. Check the divisor first.
neg
Negative remainders
-17 % 5 is -2 in C99 and later; the sign follows the left operand.
Remember
Key Takeaways
Integers:/ truncates toward zero.
Remainder:% is integer-only.
Mix: one double operand gives a double result.
One line: Arithmetic operators perform math.
Frequently Asked Questions
Cast one operand: (double)a / b. Casting after the division is too late because the integer division has already run.
It returns the remainder after integer division, so 17 % 5 is 2. It is handy for even/odd checks and wrapping indexes.
Continue with "C Relational Operators" next. Compare values with == != < > <= >= and get 1 or 0. The C Operators hub lists every topic in order.