🇯🇵 日本語 | 🇺🇸 English

Assignment & Increment

C assignment operators and increment (++) / decrement (--) explained.

📖 What to learn on this page
✅ Must-know essentials
  • = assigns, == compares
  • Compound assignments: += -= *= /=
  • ++ adds 1, -- subtracts 1
⭐ Read if you have time
  • Prefix ++i vs postfix i++
  • Incrementing the same variable twice in one expression is undefined

Assignment Operator — = Does Not Mean "equals"

a = b means "store the value of b into a". It's not the equals sign from math.
int a, b;
a = 5;        // store 5 into a
b = a + 3;    // store (a+3)=8 into b
a = a + 1;    // store (a+1)=6 into a (a was 5, now it's 6)
Unlike math: a = a + 1 would be nonsense as an equation, but in C it means "take the current value of a, add 1, and store the result back into a" — so a simply goes up by 1.

Compound Assignment Operators

Shorthand syntax for common "update a variable" patterns.
SyntaxMeaningExampleResult (when a=10)
a += ba = a + ba += 3a = 13
a -= ba = a - ba -= 4a = 6
a *= ba = a * ba *= 2a = 20
a /= ba = a / ba /= 3a = 3
a %= ba = a % ba %= 3a = 1
Get used to it: forms like sum += num; show up all over C, C++, Java, and Python.

🎮 Interactive — how compound assignment updates a

Change the operator and operands to walk through each step of the update.
① The written expression
a += 3;
② Compiler expands it
③ Substitute current a
④ Store result back in a
Variable a (memory box)
10
a holds the initial value. Click "Next step" to see how it expands.

🔁 Loop-style simulator — accumulating a value

Apply the same compound assignment over and over and watch a grow. It builds intuition for the classic sum += i inside a loop.
a = 0 (initial)
💡 Example: for (int i = 1; i <= n; i++) sum += i; just keeps adding i into sum. Tap the button a few times to get the feel.

Increment (++) and Decrement (--)

Dedicated operators that add or subtract 1. You'll see them most often inside for loops.
int i = 5;
i++;     // same as i = i + 1. i becomes 6
i--;     // same as i = i - 1. i back to 5
++i;     // also i=i+1 (prefix form)

for (int i = 0; i < 10; i++) { ... } // most common in for loops
i++ (postfix)
The expression's value is the original i; then i increases by 1.
++i (prefix)
i increases by 1 first; the expression's value is the new i.

Increment Demo

variable i
0

Try It Yourself — Assignment & Increment

assign.c
Output
Press "Run" to execute...
💡 A few things to try

Related Lessons

Operators
Arithmetic Operators
Visual guide to C arithmetic operators (+, -, *, /, %), including integer division pitfalls.
Operators
Comparison & Logical Operators
C comparison operators (==, !=, <, >) and logical operators (&&, ||, !).
Loops / Arrays / Strings
Loops (for / while)
How to use for and while loops in C, with visual explanations.
← Previous lesson
Lesson 9: Quiz (Arithmetic)
Next lesson →
Lesson 11: Comparison & Logical Operators

Review Quiz

Check your understanding of this lesson!

Q1. After int a = 5; a += 3; what is a?

5
3
8

a += 3 is equivalent to a = a + 3, so 5 + 3 = 8.

Q2. Given int a = 5; int b = a++; what is b?

5
6
4

a++ is post-increment: the current value of a (5) is assigned to b first, and then a becomes 6.

Q3. Given int a = 5; int b = ++a; what is b?

5
6
4

++a is pre-increment: a becomes 6 first, and then that new value is assigned to b.

Share this article
Share on X (Twitter) Share on Facebook Share on LINE Hatena Bookmark