πŸ‡―πŸ‡΅ ζ—₯本θͺž | πŸ‡ΊπŸ‡Έ English
Advertisement

Assignment & Increment

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

Assignment Operator — = Does Not Mean "equal"

a = b means "store the value of b into a". It is not the mathematical equals sign.
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 6)
Unlike math: a = a + 1 makes no sense mathematically, but in C it means "take the current a, add 1, store it back into a", so the value increases by 1.

Compound Assignment Operators

Shorter syntax for common "update a variable" operations.
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; are common across C/C++/Java/Python.

Increment (++) and Decrement (--)

Dedicated operators that add or subtract 1. Especially common in 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)
value of the expression is original i, then +1
++i (prefix)
+1 first, then the value of the expression

Increment Demo

variable i
0

Try It Yourself — Assignment & Increment

assign.c
Output
Press "Run" to execute...
Advertisement

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
Arithmetic Quiz
Next Lesson β†’
Comparison & Logical Operators

Check Your Understanding

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, so the current value of a (5) is assigned to b first, then a becomes 6.

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

5
6
4

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

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

Recommended Books for This Lesson

Learning the concepts here + doing exercises in a book is very effective

πŸ“˜
Suffer Through C
MMGames
A classic introductory book for beginners. Careful explanations cover the fundamentals.
View on Amazon
πŸ“—
New Clear C Programming: Introduction
Bohyoh Shibata
Plenty of diagrams and practice problems. Widely used as a university textbook.
View on Amazon
πŸ“™
The C Programming Language (2nd Edition)
B.W. Kernighan, D.M. Ritchie
Known as K&R. The original C text. Perfect for the next step after basics.
View on Amazon

* These are affiliate links. Your purchases help support this site.