Arithmetic & Assignment Operators
Calculating in CC can add, subtract, multiply, divide and find remainders — but the type of the numbers decides the answer:
7 / 2 is 3. In the lab below you watch expressions being worked out one operation at a time,
change a variable with +=, -=, *=, and see exactly when i++ and ++i change i.
- 7 / 2is 3 — 7.0 / 2 is 3.5
- -7 % 2is -1 (sign of the left side)
- i++uses i, then adds 1
Ready
Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode
The ideas behind the lab
Exactly what you watched happen, in plain steps.
Operators work on operands
An expression is made of operands (values, variables) and operators (+ - * / % …). C works it out one operation at a time and every result has a TYPE: two ints give an int, and as soon as a double is involved the result is a double. That is why 7 / 2 is 3 but 7.0 / 2 is 3.5.
int a = 7, b = 2; a + b 9 a - b 5 a * b 14 a / b 3 int / int → int a % b 1 remainder (double) a / b 3.5 a double is involved -a -7 unary minus
| Operator | Name | Example (a = 7, b = 2) | Result | Note |
|---|---|---|---|---|
| + | addition | a + b | 9 | |
| - | subtraction | a - b | 5 | also unary minus: -a |
| * | multiplication | a * b | 14 | |
| / | division | a / b | 3 | int / int cuts the decimals; 7.0 / 2 = 3.5 |
| % | remainder (modulo) | a % b | 1 | ints only; sign of the left side |
| = | assignment | a = 5 | a is 5 | stores the right side in the left |
| += -= *= /= %= | compound assignment | a += 3 | a is 10 | a = a + 3 |
| ++ | increment | a++ or ++a | a is 8 | postfix uses the old value |
| -- | decrement | a-- or --a | a is 6 | prefix uses the new value |
Division and remainder
- int / int is integer division: 7 / 2 = 3, 1 / 2 = 0. The decimals are cut, never rounded.
- With a negative result C cuts toward zero: -7 / 2 = -3.
- a % b is the remainder: 7 % 2 = 1, 10 % 5 = 0. Only for whole numbers.
- The remainder has the sign of the left side: -7 % 2 = -1, 7 % -2 = 1.
- Dividing an int by 0 crashes the program. A double divided by 0 gives inf.
Types in arithmetic
- int op int → int. If one operand is a double, the other is converted and the result is a double.
- For a real average write (a + b) / 2.0 or (double) (a + b) / 2.
- An int has limits (±2147483647). Going past them OVERFLOWS: the value wraps — undefined behaviour.
- * / % are done before + -; equal operators go from left to right: 1 / 2 * 10 = 0.
- Parentheses change the order: (a + b) * 3.
Assignment and ++ / --
- = stores a value: the right side is worked out first, then stored in the variable on the left.
- x = x + 1 means "increase x by 1"; x += 1, x++ and ++x do the same as statements.
- Postfix x++: the expression gives the OLD value, then x changes.
- Prefix ++x: x changes first, and the expression gives the NEW value.
- Never change the same variable twice in one expression (i = i++ is undefined).
Everyday calculations with operators
Every line, explained.
#include <stdio.h>
int main(void)
{
int total = 135;
135 minutes.
int hours = total / 60;
Integer division: 135 / 60 = 2 whole hours.
int minutes = total % 60;
The remainder: 135 % 60 = 15 minutes left over.
int n = 4729;
int last = n % 10;
% 10 gives the last digit: 9.
int rest = n / 10;
/ 10 removes the last digit: 472.
double avg = (85 + 90 + 72) / 3.0;
The parentheses add first (247); 3.0 makes it a real division: 82.333…
int count = 0;
count++;
Increment: count is 1.
count += 10;
Compound assignment: count is 11.
printf("%dh %dmin, %d %d, %.2f, %d\n", hours, minutes, last, rest, avg, count);
Prints 2h 15min, 9 472, 82.33, 11
return 0;
}
Spot the bug
Mistakes almost every beginner makes — and how to fix them.
An average with integer division
int a = 7, b = 8; double avg = (a + b) / 2; // 7.0
int a = 7, b = 8; double avg = (a + b) / 2.0; // 7.5
(a + b) / 2 is int / int, so the .5 is lost BEFORE the value reaches the double.
=+ instead of +=
total =+ 5; // total = +5
total += 5; // total = total + 5
=+ is read as = followed by a unary plus, so total simply becomes 5. The compiler does not complain.
% with a double
double x = 7.5; double r = x % 2;
#include <math.h> double r = fmod(x, 2); // 1.5
% works only with whole numbers — "invalid operands to binary %". Use fmod() for doubles.
int overflow
int area = 50000 * 50000; // wrong value
long long area = 50000LL * 50000;
2,500,000,000 is bigger than the largest int (2,147,483,647). The int calculation overflows before it is stored.
Changing a variable twice in one expression
i = i++; x = i++ + ++i;
i++; x = 2 * i + 1; // write what you mean
The C standard does not say in which order these changes happen — undefined behaviour. Different compilers give different results.
Check your understanding
Answer each question — you will see the explanation straight away.
What is 17 / 5 in C (both int)?
Integer division cuts the decimals: 3.
What is 17 % 5?
17 = 5 × 3 + 2, so the remainder is 2.
What is 2 + 3 * 4?
* is done before +: 3 * 4 = 12, then 2 + 12 = 14.
int i = 3; int j = i++; — what are i and j?
Postfix: j gets the old value 3, then i becomes 4.
int x = 10; x -= 4; x *= 2; — what is x?
x -= 4 → 6, then x *= 2 → 12.
Arithmetic & Assignment Operators quiz
Test yourself: 10 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.