Skip to content
StudyFame

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 + 3 is not x * 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.

OperatorExampleSame asIf x = 10, result
=x = 5Store 5 in x5
+=x += 5x = x + 515
-=x -= 3x = x - 37
*=x *= 2x = x * 220
/=x /= 4x = x / 42
%=x %= 3x = x % 31

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, first b = 0, then a = b.
  • The right side of a compound operator is calculated fully first: x *= 2 + 3 means x = 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

  1. Start: x = 10.
  2. x += 5: 10 + 5 = 15.
  3. x -= 3: 15 − 3 = 12.
  4. x *= 2: 12 × 2 = 24.
  5. x /= 4: 24 / 4 = 6.
  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 = 5 works: b = 5 gives 5, which is then stored in a.
  • 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= y means x = x op (y).
  • Assignment goes right to left.
  • The left side must be a variable (an lvalue).
  • x *= 2 + 3 is x × 5, not x × 2 + 3.

Common Mistakes Beginners Make

MistakeWhat happensFix
x =+ 5;Sets x to +5 (no adding)x += 5;
5 = x;Error: lvalue requiredx = 5;
x + = 5; (space)Syntax errorNo space: +=
x /= 0;Runtime crashCheck the divisor first

Assignment Operators Practice Questions

  1. What is a compound assignment operator? Give two examples.
  2. What is the value of x after int x = 8; x -= 3; x *= 4;?
  3. What is the value of x after int x = 3; x *= 2 + 1;?
  4. What is the difference between x += 1 and x =+ 1?
  5. In which direction does assignment work?
Show answers
  1. An operator that does an operation and assigns the result, e.g. +=, *=.
  2. 20 (8 − 3 = 5, 5 × 4 = 20).
  3. 9 (x = 3 × (2 + 1)).
  4. x += 1 adds 1 to x; x =+ 1 sets x to +1.
  5. 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)

  1. x += 3 is the same as:

    A) x = 3   B) x = x + 3   C) x == x + 3   D) x = x * 3

  2. If x = 9, what is x after x %= 4?

    A) 2   B) 1   C) 4   D) 0

  3. Assignment associativity is:

    A) Left to right   B) Right to left   C) None   D) Random

  4. If x = 2, what is x after x *= 3 + 1?

    A) 7   B) 8   C) 6   D) 9

  5. Which is invalid?

    A) a = b = 5;   B) a += 2;   C) 10 = a;   D) a %= 2;

Show answers
  1. B: x = x + 3
  2. B: 1.
  3. B: right to left.
  4. B: 8 (2 × 4).
  5. 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= yx = x op (y)
AssociativityRight to left
Left sideMust be a variable (lvalue)
x *= 2 + 3x = 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 =+ 5 just 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.