Expression evaluator Ready
main.c

                

Ready

Statements run 0 Lines printed 0
Ready

Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode

How it works

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
Arithmetic and assignment operators
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

  1. int / int is integer division: 7 / 2 = 3, 1 / 2 = 0. The decimals are cut, never rounded.
  2. With a negative result C cuts toward zero: -7 / 2 = -3.
  3. a % b is the remainder: 7 % 2 = 1, 10 % 5 = 0. Only for whole numbers.
  4. The remainder has the sign of the left side: -7 % 2 = -1, 7 % -2 = 1.
  5. Dividing an int by 0 crashes the program. A double divided by 0 gives inf.

Types in arithmetic

  1. int op int → int. If one operand is a double, the other is converted and the result is a double.
  2. For a real average write (a + b) / 2.0 or (double) (a + b) / 2.
  3. An int has limits (±2147483647). Going past them OVERFLOWS: the value wraps — undefined behaviour.
  4. * / % are done before + -; equal operators go from left to right: 1 / 2 * 10 = 0.
  5. Parentheses change the order: (a + b) * 3.

Assignment and ++ / --

  1. = stores a value: the right side is worked out first, then stored in the variable on the left.
  2. x = x + 1 means "increase x by 1"; x += 1, x++ and ++x do the same as statements.
  3. Postfix x++: the expression gives the OLD value, then x changes.
  4. Prefix ++x: x changes first, and the expression gives the NEW value.
  5. Never change the same variable twice in one expression (i = i++ is undefined).
Walkthrough

Everyday calculations with operators

Every line, explained.

1 #include <stdio.h>
2
3 int main(void)
4 {
5 int total = 135; 135 minutes.
6 int hours = total / 60; Integer division: 135 / 60 = 2 whole hours.
7 int minutes = total % 60; The remainder: 135 % 60 = 15 minutes left over.
8 int n = 4729;
9 int last = n % 10; % 10 gives the last digit: 9.
10 int rest = n / 10; / 10 removes the last digit: 472.
11 double avg = (85 + 90 + 72) / 3.0; The parentheses add first (247); 3.0 makes it a real division: 82.333…
12 int count = 0;
13 count++; Increment: count is 1.
14 count += 10; Compound assignment: count is 11.
15 printf("%dh %dmin, %d %d, %.2f, %d\n", hours, minutes, last, rest, avg, count); Prints 2h 15min, 9 472, 82.33, 11
16 return 0;
17 }
Common mistakes

Spot the bug

Mistakes almost every beginner makes — and how to fix them.

An average with integer division

✗ Wrong
int a = 7, b = 8;
double avg = (a + b) / 2;     // 7.0
✓ Right
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 +=

✗ Wrong
total =+ 5;    // total = +5
✓ Right
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

✗ Wrong
double x = 7.5;
double r = x % 2;
✓ Right
#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

✗ Wrong
int area = 50000 * 50000;   // wrong value
✓ Right
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

✗ Wrong
i = i++;
x = i++ + ++i;
✓ Right
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.

Practice

Check your understanding

Answer each question — you will see the explanation straight away.

0 / 5 correct
Question 1

What is 17 / 5 in C (both int)?

Question 2

What is 17 % 5?

Question 3

What is 2 + 3 * 4?

Question 4

int i = 3; int j = i++; — what are i and j?

Question 5

int x = 10; x -= 4; x *= 2; — what is x?

Assessment

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.