1. Simple Assignment Operator (=)
The = operator stores a value into a variable. It is the most basic
assignment operator and the one you will use the most.
The shape is simple:
Left_Operand = Right_Operand;
The value on the right side gets evaluated first, then stored in the variable on the left.
Example 1: Simple assignment in C
#include <stdio.h>
int main()
{
int x = 10;
int y = x; /* y gets a copy of x's value */
printf("x = %d\n", x);
printf("y = %d\n", y);
return 0;
}
Output:
x = 10
y = 10
Both x and y hold 10. Notice that y does not
become linked to x โ it just copies the value. Change x
later and y stays at 10.
= here is assignment, not equality.
C uses == for comparison. Writing if (x = 5) assigns 5 to
x and always evaluates to true. That is a bug, not a comparison.
2. += Add and Assign
The += operator adds the right-hand value to the variable and stores the
result back. Writing x += y is exactly the same as writing
x = x + y, just shorter.
Example 2: Using += in C
#include <stdio.h>
int main()
{
int x = 10;
int y = 10;
x += y; /* same as x = x + y */
printf("x = %d\n", x);
return 0;
}
Output:
x = 20
What happens step by step
| Step | Action | x | y |
|---|---|---|---|
| 1 | Initialize | 10 | 10 |
| 2 | Evaluate x + y | 20 | 10 |
| 3 | Assign result to x | 20 | 10 |
When to use +=
- Adding to a running total:
total += price; - Counting:
count += 1; - Building sums in loops
3. -= Subtract and Assign
The -= operator subtracts the right-hand value from the variable and
stores the result back. p -= q is the same as p = p - q.
Example 3: Using -= in C
#include <stdio.h>
int main()
{
int p = 10;
int q = 3;
p -= q; /* same as p = p - q */
printf("p = %d\n", p);
return 0;
}
Output:
p = 7
What happens step by step
| Step | Action | p | q |
|---|---|---|---|
| 1 | Initialize | 10 | 3 |
| 2 | Evaluate p - q | 7 | 3 |
| 3 | Assign result to p | 7 | 3 |
When to use -=
- Deducting from a balance:
balance -= withdrawal; - Countdowns:
time_left -= 1; - Shrinking a quantity as items are consumed
4. *= Multiply and Assign
The *= operator multiplies the variable by the right-hand value and
stores the result back. x *= y is the same as x = x * y.
Example 4: Using *= in C
#include <stdio.h>
int main()
{
int x = 10;
int y = 3;
x *= y; /* same as x = x * y */
printf("x = %d\n", x);
return 0;
}
Output:
x = 30
What happens step by step
| Step | Action | x | y |
|---|---|---|---|
| 1 | Initialize | 10 | 3 |
| 2 | Evaluate x * y | 30 | 3 |
| 3 | Assign result to x | 30 | 3 |
When to use *=
- Doubling or scaling a value repeatedly
- Compounding growth in a loop:
amount *= rate; - Converting units where a constant scale factor applies
5. /= Divide and Assign
The /= operator divides the variable by the right-hand value and stores
the result back. a /= b is the same as a = a / b.
One important thing: if both sides are integers, C does integer division and drops
the decimal part.
Example 5: Using /= in C
#include <stdio.h>
int main()
{
int a = 8;
int b = 2;
a /= b; /* same as a = a / b */
printf("a = %d\n", a);
return 0;
}
Output:
a = 4
What happens step by step
| Step | Action | a | b |
|---|---|---|---|
| 1 | Initialize | 8 | 2 |
| 2 | Evaluate a / b | 4 | 2 |
| 3 | Assign result to a | 4 | 2 |
When to use /=
- Halving a value:
x /= 2; - Computing an average:
avg /= count; - Unit conversions that divide by a factor
โ ๏ธ Integer division trap: If a = 5 and
b = 2, then a /= b leaves a = 2, not 2.5.
Cast one operand to float if you need the fractional part.
6. %= Modulus and Assign
The %= operator divides the variable by the right-hand value and stores
the remainder back. a %= b is the same as a = a % b.
It works only with integers.
Example 6: Using %= in C
#include <stdio.h>
int main()
{
int a = 7;
int b = 2;
a %= b; /* same as a = a % b */
printf("a = %d\n", a);
return 0;
}
Output:
a = 1
What happens step by step
| Step | Action | a | b |
|---|---|---|---|
| 1 | Initialize | 7 | 2 |
| 2 | Evaluate a % b (remainder of 7 รท 2) | 1 | 2 |
| 3 | Assign remainder to a | 1 | 2 |
When to use %=
- Cycling an index:
index %= size; - Checking divisibility (after
a %= 2,ais 0 or 1) - Splitting numbers into digits in a loop
7. <<= and >>= Shift and Assign
These two operators work on the binary representation of a number. They shift the bits left or right by a given number of positions and store the result back.
The shape:
a <<= b; /* same as a = a << b */
a >>= b; /* same as a = a >> b */
Shifting left by 1 doubles the number. Shifting right by 1 halves it (dropping the remainder). You will not use these often in everyday C code, but they show up in performance-sensitive code and low-level work.
Example 7: Left shift and assign (<<=)
/* C program using <<= operator */
#include <stdio.h>
int main()
{
int a = 7; /* 7 in binary is 0111 */
int b = 1;
a <<= b; /* same as a = a << b */
printf("a = %d\n", a);
return 0;
}
Output:
a = 14
7 in binary is 0111. Shift the bits one position left and you get
1110, which is 14 in decimal. Doubling 7 gives 14.
Example 8: Right shift and assign (>>=)
/* C program using >>= operator */
#include <stdio.h>
int main()
{
int a = 7; /* 7 in binary is 0111 */
int b = 1;
a >>= b; /* same as a = a >> b */
printf("a = %d\n", a);
return 0;
}
Output:
a = 3
Shift 0111 one position right and you get 0011, which is 3.
Halving 7 and dropping the remainder gives 3.
When to use <<= and >>=
- Fast multiplication or division by powers of two
- Bit manipulation in flags and masks
- Reading or packing binary data
Operator Precedence (Where Assignment Fits)
Assignment has the lowest precedence in C โ only the comma operator sits below it.
Everything on the right side gets evaluated before the value is stored. That is why
a = b + c * 2; computes b + (c * 2) first, then assigns.
| Precedence | Operators | Associativity |
|---|---|---|
| High | * / % | Left to right |
| Medium | + - | Left to right |
| Low | << >> & ^ | | Left to right |
| Lower | && || | Left to right |
| Lowest | = += -= *= /= %= &= |= ^= <<= >>= | Right to left |