Topic 26 of 100 Operators in C Interactive Tutorial

Assignment Operators in C: Basic and Compound Assignment

10 mins read Last updated: 22 Aug 2026 17,200 views
C Compiler 0.00%

What is Assignment Operators in C? Core Concept & Direct Summary

Core Objective & Problem Statement

How do simple (`=`) and compound assignment operators (`+=`, `-=`, `*=`, `/=`, `%=`, `&=`, `|=`, `^=`, `<<=`, `>>=`) work?

Direct Answer Summary

Here's a line of code you've written a hundred times without a second thought:

That humble=is the most-used operator in all of programming. Every value stored, every counter updated, every result saved — all of it flows through assignment. And yet this "simple" operator hides genuine subtleties: it works right-to-left, it secretly returns a value, and it comes in eleven compound flavors that working programmers type daily.

In this guide, you'll learn how assignment really works in C, the complete compound operator family, the famous=vs==trap from the assignment side of the fence, and the shortcuts that make your code cleaner.

Let's assign some values.

How Does Assignment Operators in C Work? In-Depth Mechanics & Architecture

The Basic Assignment Operator (=)

The=operator takes the value on therightand stores it in the variable on theleft:

main.c
Run Online
int marks;
// declare the box
marks = 85;
// put 85 into it

Two rules govern everything:

  1. Flow is right → left.The right side is calculated first; the result lands in the left side.
  2. The left side must be a container.Only something that canholda value (a variable, an array slot, a dereferenced pointer) can sit left of=. Programmers call this anlvalue— "left value."
main.c
Run Online
int x = 10;
x = 20;
// ✅ x is a container
x + 5 = 20;
// ❌ ERROR — x + 5 is a calculation, not a box
10 = x;
// ❌ ERROR — you can't store into a literal

That second rule is why10 = xfails: a number literal has no memory slot to fill. The lvalue rule is the compiler's way of asking,"Where exactly should I put this?"

Assignment Is an Expression (The Sneaky Detail)

Here's what most tutorials skip:an assignment itself has a value— the value that was assigned. That's why chained assignment works:

main.c
Run Online
int a, b, c;
a = b = c = 42;
// legal! works right to left

C reads it asa = (b = (c = 42)):cbecomes 42 (and thatexpressionis 42), sobbecomes 42, thena. All three end up holding 42. This also explains whyif (x = 5)compiles — the assignment evaluates to 5, which is non-zero, which is true. The classic bug, explained by one rule.

The Compound Assignment Operators: C's Shorthand

Watch the pattern in everyday code:

main.c
Run Online
score = score + 50;
// read score, add 50, store back
balance = balance - fee;
total = total * count;

"Take the variable, operate on it, store it back" is so common that C built dedicated shortcuts —compound assignment operators:

OperatorExampleEquivalent ToResult (if x = 10)
+=x += 5x = x + 515
-=x -= 5x = x - 55
*=x *= 5x = x * 550
/=x /= 5x = x / 52
%=x %= 5x = x % 50
<<=x <<= 1x = x << 120
>>=x >>= 1x = x >> 15
&=x &= 6x = x & 62
|=x |= 6x = x | 614
^=x ^= 6x = x ^ 612
main.c
Run Online
#include <stdio.h>
int main() {
    int score = 100;
    score += 50;
    // 150 — collect bonus
    score -= 30;
    // 120 — pay penalty
    score *= 2;
    // 240 — double or nothing!
    score /= 3;
    // 80  — integer division (watch the trap!)
    score %= 7;
    // 3   — remainder of 80 / 7
    printf("Final score: %d\n", score);
    // 3
    return 0;
}

Same arithmetic rules apply underneath —/=on integers still truncates,%=still returns the remainder. Compound operators change thesyntax, not the math.

Why Compound Operators Are Worth Using

Three honest reasons professionals preferx += 5overx = x + 5:

1. Less repetition.The variable appears once — less typing, and no chance ofscore = scor + 5typos that compile into brand-new bugs.

2. Clearer intent.count += 1instantly reads as "increment this thing" — the reader doesn't have to verify both sides mention the same variable.

3. It really matters with long expressions:

main.c
Run Online
studentRecords[classId][studentId].marks += bonusPoints;
// vs repeating that whole left side twice — imagine the typo risk

When the left side is complicated, compound assignment isn't just style — it's safety.

The Traps: = vs ==, Chaining, and Division

Trap 1: Assignment Is Not Comparison

From this side of the fence, the famous bug looks like this: youmeantto compare, but typed assign:

main.c
Run Online
int lives = 3;
if (lives = 0) {
    // 😱 assigns 0 — and 0 is FALSE
    printf("Game over!\n");
    // never runs — backwards from intent!
}
// lives is now 0 — silently destroyed

if (lives = 0)assigns zero, evaluates to zero, and the block never runs. Two bugs in five characters. The fix remains eternal vigilance (or the Yoda habitif (0 == lives)):comparison needs==, full stop.

Trap 2: Compound Division Still Truncates

main.c
Run Online
int average = 95;
average /= 4;
// 23 — NOT 23.75! Integer division applies here too

/=inherits all of/'s integer behavior. For decimal results, the variable itself must be a floating type:

main.c
Run Online
double average = 95;
average /= 4;
// 23.75 ✅

Trap 3: Modulus Compound Needs Integers

Just like%, the%=operator refuses floating-point operands:

main.c
Run Online
double d = 10.5;
// d %= 3;
// ❌ ERROR — modulus is integers-only

A Complete Working Example: Bank Account Simulator

main.c
Run Online
#include <stdio.h>
int main() {
    double balance = 1000.0;
    printf("Opening balance: %.2f\n", balance);
    balance += 500;
    // salary credited
    printf("After deposit:  %.2f\n", balance);
    balance -= 199.99;
    // online shopping
    printf("After shopping: %.2f\n", balance);
    balance *= 1.02;
    // 2% interest
    printf("After interest: %.2f\n", balance);
    int wholeRupees =(int) balance;
    wholeRupees %= 100;
    // last two digits, just because we can
    printf("Last two digits: %d\n", wholeRupees);
    return 0;
}

Output:

main.c
Run Online
Opening balance: 1000.00
After deposit: 1500.00
After shopping: 1300.01
After interest: 1326.01
Last two digits: 26

Four compound operators doing everyday work — deposit, spend, grow, extract — in a program that reads almost like the bank statement it describes.

Runnable C Code Example & Practical Implementation

main.c
Run Online
#include <stdio.h>
int main() {
    int balance = 1000;
    balance += 250;
    // balance = balance + 250
    balance -= 50;
    // balance = balance - 50
    balance *= 2;
    // balance = balance * 2
    printf("Final Balance: $%d\
", balance);
    return 0;
}

Key Takeaways, Best Practices & Memory Safety Rules

Best Practice & Key Takeaways
Use compound assignment operators for concise, readable modifications of state variables.

Frequently Asked Questions & Technical Interview Insights

Technical Interview & Viva Questions

Frequently asked C programming interview questions
Q

Q: Is `x += 1` functionally different from `x = x + 1`?

A: For simple variables they produce identical results. However, in complex expressions like `arr[getIndex()] += 1`, `getIndex()` is evaluated only once in compound assignment, whereas in `arr[getIndex()] = arr[getIndex()] + 1` it is evaluated twice.
Q

Q: Can you chain assignments in C?

A: Yes, `int a, b, c; a = b = c = 5;` assigns 5 to all three variables.

Related Tools, Tutorials & Authoritative References

Recommended Interactive Tools

Authoritative Standards & Citations