The C Path — learn C, visually

🌱 C Basics

Arithmetic: +, −, ×, ÷ and the truncation trap

⏱ 13 min · free interactive lesson · quizzes, visualizations & a real compiler

▶ Open the interactive lesson — free, no signup
Why you're learning this

Ask C to average the test scores 90 and 91 and it confidently answers 90 — the .5 silently vanishes. That quiet trap has skewed grades, prices, and game scores in real software. Here you'll learn all of C's math, exactly where that trap hides, and the one-character fix that defuses it.

Time to compute. C's arithmetic operators look like grade-school math — + - * / — plus one newcomer, % (modulo, the remainder of a division). The values an operator works on are called its operands — and when both operands are whole numbers, C hides a sharp edge in the most innocent-looking operator: division.

The big five

arith.c
#include <stdio.h>

int main(void) {
    int a = 7, b = 2;

    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   <- surprise!\n", a / b);
    printf("a %% b = %d   (the remainder)\n", a % b);
    printf("a / 2.0 = %f\n", a / 2.0);
    return 0;
}
terminal
$ gcc arith.c -o arith && ./arith
a + b = 9
a - b = 5
a * b = 14
a / b = 3   <- surprise!
a % b = 1   (the remainder)
a / 2.0 = 3.500000
💀

The integer division trap: 7 / 2 is 3, not 3.5! When both operands are integers, C performs integer division and truncates toward zero — the fraction is thrown away. This bites everyone: (a + b) / 2 for an average, celsius * 9 / 5… If you want 3.5, make one side floating point: 7 / 2.0 or 7.0 / 2.

% gives what division threw away: 7 % 2 is 1. It's the workhorse of "is this even?", "wrap this index around", and clock arithmetic. Since C99, the sign of the result follows the dividend: -7 % 2 is -1. And x % 0, like x / 0, is undefined behavior — no friendly exception, just chaos.

🧠 Checkpoint: What does int avg = (90 + 91) / 2; store?

  • 90.5
  • 90
  • 91
  • undefined behavior
Show answer

90 — 90 + 91 = 181; both operands of / are ints, so 181 / 2 truncates toward zero: 90. The .5 is silently discarded. To keep it, divide by 2.0 and store in a double.

++ and −−: the increment twins

Adding or subtracting 1 is so common that C gives it dedicated operators — the very ones that named C++. Each comes in two flavors, and the difference is when you see the new value:

Step through it — watch the variable panel closely:

This spot has an interactive trace widget — open the interactive lesson to play with it.

⚠️

Never modify a variable twice in one expression. i = i++; and x = i++ + ++i; are undefined behavior — the standard doesn't say which happens first, and compilers genuinely produce different answers. If code looks like a puzzle, it's a bug.

🧠 Checkpoint: After int n = 10; int m = n--;, what are n and m?

  • n=9, m=9
  • n=10, m=9
  • n=9, m=10
  • n=10, m=10
Show answer

n=9, m=10 — Postfix decrement: m gets the old value (10), then n drops to 9. With prefix (m = --n;) both would be 9.

Compound assignment: the += family

x = x + 5 is so common it has a shorthand: x += 5. Every arithmetic (and bitwise) operator has one — -=, *=, /=, %=. They say "update this variable using its own value", and they say it without repeating the name:

compound.c
#include <stdio.h>

int main(void) {
    int score = 100;

    score += 50;    /* score = score + 50  -> 150 */
    score -= 30;    /* -> 120 */
    score *= 2;     /* -> 240 */
    score /= 10;    /* -> 24  */
    score %= 5;     /* -> 4   */

    printf("final score: %d\n", score);
    return 0;
}
terminal
$ gcc compound.c -o compound && ./compound
final score: 4

Who goes first? Precedence

Just like in math, 2 + 3 * 4 is 14, not 20 — multiplication binds tighter. The arithmetic pecking order:

priorityoperatorsnote
1 (tightest)(), x++, x--parentheses always win
2unary -, ++x, --xas in -x
3* / %left to right
4+ -left to right
5 (loosest)= += -=right to left
💡

Memorizing the full 15-level C precedence table is a party trick, not a skill. When in doubt, add parentheses — they cost nothing and your readers (including future-you) will thank you.

This spot has an interactive editor widget — open the interactive lesson to play with it.

🧠 Checkpoint: What is 2 + 3 * 4 % 5?

  • 0
  • 4
  • 2
  • 14
Show answer

4* and % share a precedence level and go left to right: 3*4 = 12, then 12 % 5 = 2, then 2 + 2 = 4. If you had to think hard — that’s the argument for parentheses.

You can now compute — next you'll compare: the operators that let programs make decisions.

▶ Practice this lesson interactively (with live gcc)