Assignment Operators in C
You often need to update a variable using its own value: add 5 to the score, double the price, take 10% off. Writing score = score + 5 every time is long.
C gives you shortcuts called compound assignment operators, like score += 5. In this lesson you will learn all of them, and one trap that catches many students.
What You Will Learn
- The simple assignment operator
=. - The compound (shorthand) operators:
+=,-=,*=,/=,%=. - Chained assignment:
a = b = c = 0. - Why
x *= 2 + 3is notx * 2 + 3.
Assignment Operators in C Explained
Exam definition: Assignment operators store a value in a variable. = is the simple assignment operator. Compound assignment operators (+=, -=, *=, /=, %=) do an operation and assign the result in one step: x += 5 means x = x + 5.
| Operator | Example | Same as | If x = 10, result |
|---|---|---|---|
= | x = 5 | Store 5 in x | 5 |
+= | x += 5 | x = x + 5 | 15 |
-= | x -= 3 | x = x - 3 | 7 |
*= | x *= 2 | x = x * 2 | 20 |
/= | x /= 4 | x = x / 4 | 2 |
%= | x %= 3 | x = x % 3 | 1 |
There are also bitwise versions: &=, |=, ^=, <<=, >>=.
Memory trick: In x += 5, read it as "x plus-equals 5" = "add 5 to x". The operator comes before the =. (=+ is wrong: x =+ 5 just means x = +5.)
Rules
- The left side must be a variable (something that can store a value), not a number or an expression.
- Assignment works right to left: in
a = b = 0, firstb = 0, thena = b. - The right side of a compound operator is calculated fully first:
x *= 2 + 3meansx = x * (2 + 3).
Syntax of Assignment Operators
variable = expression;
variable op= expression; /* same as: variable = variable op (expression) */
Example 1: All Compound Assignment Operators
#include <stdio.h>
int main(void)
{
int x = 10;
x += 5; printf("x += 5 -> %d\n", x);
x -= 3; printf("x -= 3 -> %d\n", x);
x *= 2; printf("x *= 2 -> %d\n", x);
x /= 4; printf("x /= 4 -> %d\n", x);
x %= 4; printf("x %%= 4 -> %d\n", x);
return 0;
}
Output
x += 5 -> 15 x -= 3 -> 12 x *= 2 -> 24 x /= 4 -> 6 x %= 4 -> 2
Code Explanation
- Start: x = 10.
x += 5: 10 + 5 = 15.x -= 3: 15 − 3 = 12.x *= 2: 12 × 2 = 24.x /= 4: 24 / 4 = 6.x %= 4: remainder of 6 ÷ 4 = 2.
Each line uses the value left by the line before it.
How Assignment Works
- C first calculates the right side completely.
- Then it stores the result in the variable on the left side.
- An assignment is also an expression: its value is the value stored. That is why
a = b = 5works:b = 5gives 5, which is then stored ina. - For
x op= y, C reads x only once, which is short to write and easy to read.
Example 2: Chained Assignment and the *= Trap
#include <stdio.h>
int main(void)
{
int a, b, c;
a = b = c = 7; /* right to left */
printf("a = %d, b = %d, c = %d\n", a, b, c);
int x = 4;
x *= 2 + 3; /* x = x * (2 + 3) = 20 */
printf("x *= 2 + 3 -> %d\n", x);
int y = 4;
y = y * 2 + 3; /* different: 4 * 2 + 3 = 11 */
printf("y * 2 + 3 -> %d\n", y);
return 0;
}
Output
a = 7, b = 7, c = 7 x *= 2 + 3 -> 20 y * 2 + 3 -> 11
Important Points to Remember
=assigns;==compares.x op= ymeansx = x op (y).- Assignment goes right to left.
- The left side must be a variable (an lvalue).
x *= 2 + 3is x × 5, not x × 2 + 3.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
x =+ 5; | Sets x to +5 (no adding) | x += 5; |
5 = x; | Error: lvalue required | x = 5; |
x + = 5; (space) | Syntax error | No space: += |
x /= 0; | Runtime crash | Check the divisor first |
Assignment Operators Practice Questions
- What is a compound assignment operator? Give two examples.
- What is the value of x after
int x = 8; x -= 3; x *= 4;? - What is the value of x after
int x = 3; x *= 2 + 1;? - What is the difference between
x += 1andx =+ 1? - In which direction does assignment work?
Show answers
- An operator that does an operation and assigns the result, e.g.
+=,*=. - 20 (8 − 3 = 5, 5 × 4 = 20).
- 9 (x = 3 × (2 + 1)).
x += 1adds 1 to x;x =+ 1sets x to +1.- Right to left.
Coding Exercise
A shop has 500 items. Using compound operators: 120 are sold, then 80 more arrive, then the stock is split equally between 3 branches (integer division). Print the stock after each step and the remainder left at the main shop.
Show solution
#include <stdio.h>
int main(void)
{
int stock = 500;
stock -= 120; printf("After sale: %d\n", stock);
stock += 80; printf("After delivery: %d\n", stock);
int left = stock % 3;
stock /= 3; printf("Each branch: %d\n", stock);
printf("Left at main shop: %d\n", left);
return 0;
}
After sale: 380 After delivery: 460 Each branch: 153 Left at main shop: 1
Quick Quiz: Assignment Operators in C (MCQ)
-
x += 3is the same as:A)
x = 3B)x = x + 3C)x == x + 3D)x = x * 3 -
If x = 9, what is x after
x %= 4?A) 2 B) 1 C) 4 D) 0
-
Assignment associativity is:
A) Left to right B) Right to left C) None D) Random
-
If x = 2, what is x after
x *= 3 + 1?A) 7 B) 8 C) 6 D) 9
-
Which is invalid?
A)
a = b = 5;B)a += 2;C)10 = a;D)a %= 2;
Show answers
- B:
x = x + 3 - B: 1.
- B: right to left.
- B: 8 (2 × 4).
- C:
10 = a;
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Simple assignment | = |
|---|---|
| Compound | += -= *= /= %= (and &= |= ^= <<= >>=) |
x op= y | x = x op (y) |
| Associativity | Right to left |
| Left side | Must be a variable (lvalue) |
x *= 2 + 3 | x = x * 5 |
Frequently asked questions
- What is the advantage of compound assignment operators?
- They are shorter to write and the variable is written only once, so there are fewer mistakes.
- What is an lvalue?
- Something that can appear on the left of =, i.e. a variable with a memory location.
- How does a = b = c = 0 work?
- Assignment is right to left: c = 0 first, then b = c, then a = b.
- Is x =+ 5 the same as x += 5?
- No.
x =+ 5just sets x to +5.
Remember
- Operator before the equals sign:
+=, not=+. - Right side is calculated fully first.
Next Topic
Next lesson: Increment and Decrement Operators in C. You will learn ++ and --, and the famous difference between i++ and ++i.