C Notes
Learn all assignment operators in C including simple assignment, compound assignment operators (+=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=). Understand their syntax, behavior, and practical usage.
Assignment operators store values in variables. While the simple assignment (=) is something every beginner uses from day one, C also provides compound assignment operators that combine an arithmetic or bitwise operation with assignment into a single, concise expression. They make code shorter, more readable, and in some cases, can be slightly more efficient.
Simple Assignment Operator (=)
The basic assignment operator stores the value of the right-hand expression into the left-hand variable. The left side must always be an lvalue — something that has a memory address (like a variable).
#include <stdio.h>
int main() {
int x = 10; // Initialize x with 10
float pi = 3.14; // Initialize pi
char ch = 'A'; // Initialize ch
x = 25; // Reassign x
printf("x = %d\n", x);
// Assignment returns the assigned value
int a, b, c;
a = b = c = 100; // Right-to-left associativity
printf("a=%d, b=%d, c=%d\n", a, b, c);
// Assignment in expressions
int result;
printf("Assigned: %d\n", result = 42);
return 0;
}x = 25 a=100, b=100, c=100 Assigned: 42
Important: The assignment operator has right-to-left associativity. In a = b = c = 100, it's evaluated as a = (b = (c = 100)).
Compound Assignment Operators
Compound assignment operators perform an operation and assignment in one step. The general form is:
This is equivalent to:
| Operator | Example | Equivalent To | |||
|---|---|---|---|---|---|
+= | x += 5 | x = x + 5 | |||
-= | x -= 3 | x = x - 3 | |||
*= | x *= 2 | x = x * 2 | |||
/= | x /= 4 | x = x / 4 | |||
%= | x %= 3 | x = x % 3 | |||
&= | x &= 0xF | x = x & 0xF | |||
| ` | =` | `x | = 0x1` | `x = x | 0x1` |
^= | x ^= mask | x = x ^ mask | |||
<<= | x <<= 2 | x = x << 2 | |||
>>= | x >>= 1 | x = x >> 1 |
Arithmetic Compound Assignment
#include <stdio.h>
int main() {
int x = 20;
x += 5; // x = x + 5 = 25
printf("After += 5: %d\n", x);
x -= 3; // x = x - 3 = 22
printf("After -= 3: %d\n", x);
x *= 2; // x = x * 2 = 44
printf("After *= 2: %d\n", x);
x /= 4; // x = x / 4 = 11
printf("After /= 4: %d\n", x);
x %= 3; // x = x % 3 = 2
printf("After %%= 3: %d\n", x);
return 0;
}After += 5: 25 After -= 3: 22 After *= 2: 44 After /= 4: 11 After %= 3: 2
Bitwise Compound Assignment
#include <stdio.h>
int main() {
unsigned int flags = 0xFF; // 11111111
flags &= 0x0F; // Clear upper nibble: 00001111
printf("After &= 0x0F: 0x%02X\n", flags);
flags |= 0x30; // Set bits 4 and 5: 00111111
printf("After |= 0x30: 0x%02X\n", flags);
flags ^= 0x05; // Toggle bits 0 and 2: 00111010
printf("After ^= 0x05: 0x%02X\n", flags);
unsigned int num = 8; // 1000
num <<= 2; // Left shift by 2: 100000 = 32
printf("8 <<= 2: %u\n", num);
num >>= 3; // Right shift by 3: 100 = 4
printf("32 >>= 3: %u\n", num);
return 0;
}After &= 0x0F: 0x0F After |= 0x30: 0x3F After ^= 0x05: 0x3A 8 <<= 2: 32 32 >>= 3: 4
Why Use Compound Assignment?
There are several advantages beyond just saving keystrokes:
1. Readability
// Less readable
totalSalesForCurrentQuarter = totalSalesForCurrentQuarter + monthlySales;
// More readable
totalSalesForCurrentQuarter += monthlySales;2. Evaluates Left Side Only Once
This matters when the left side has side effects:
getIndex called arr[0] = 110
Practical Examples
Running Total
Total: $196.49 Average: $39.30
Power Calculation
2^10 = 1024
String Processing with +=
Vowels: 3, Consonants: 7, Spaces: 1
Type Conversion in Assignment
When you assign a value of one type to a variable of another type, implicit conversion occurs:
#include <stdio.h>
int main() {
// Float to int: truncation
int a;
a = 9.87;
printf("int = 9.87 → %d\n", a);
// Int to float: no loss (within precision)
float b;
b = 42;
printf("float = 42 → %f\n", b);
// Double to float: possible precision loss
float c;
c = 3.141592653589793;
printf("float = pi → %.15f\n", c);
// Large int to char: truncation
char d;
d = 321; // 321 % 256 = 65 = 'A'
printf("char = 321 → '%c' (%d)\n", d, d);
return 0;
}int = 9.87 → 9 float = 42 → 42.000000 float = pi → 3.141592741012573 char = 321 → 'A' (65)
Common Mistakes
Confusing = with ==
#include <stdio.h>
int main() {
int x = 5;
// Bug: assigns 0 to x, condition is false
if (x = 0) {
printf("This never prints\n");
}
// Bug: assigns 10 to x, condition is always true
x = 5;
if (x = 10) {
printf("This always prints! x is now %d\n", x);
}
return 0;
}This always prints! x is now 10
Compound Assignment Precedence
Remember that x *= a + b means x = x * (a + b), NOT x = x * a + b:
#include <stdio.h>
int main() {
int x = 3;
x *= 2 + 4; // x = 3 * (2 + 4) = 3 * 6 = 18
printf("x *= 2 + 4: %d\n", x);
int y = 3;
y = y * 2 + 4; // y = 6 + 4 = 10 (different!)
printf("y = y * 2 + 4: %d\n", y);
return 0;
}x *= 2 + 4: 18 y = y * 2 + 4: 10
Assignment Operator Precedence
Assignment operators have very low precedence — almost everything else is evaluated first. They also associate right-to-left:
| Priority | Operators | |
|---|---|---|
| Very Low | =, +=, -=, *=, /=, %=, &=, ` | =, ^=, <<=, >>=` |
#include <stdio.h>
int main() {
int a, b;
// Assignment has lower precedence than comparison
a = 5 > 3; // a = (5 > 3) = 1
printf("a = 5 > 3: %d\n", a);
// Right-to-left associativity
a = b = 10 + 5; // a = (b = 15) → both become 15
printf("a=%d, b=%d\n", a, b);
return 0;
}a = 5 > 3: 1 a=15, b=15
Interview Questions
Q1: What is the difference between = and == in C?
= is the assignment operator — it stores a value in a variable. == is the equality operator — it compares two values and returns 1 (true) or 0 (false). Using = in an if condition is a common bug that assigns instead of comparing.
**Q2: What does x *= y + z expand to?**
It expands to x = x * (y + z), NOT x = x * y + z. The entire right-hand side expression is treated as one operand, so the addition happens before the multiplication with x.
Q3: Why is a[i++] += 5 preferred over a[i++] = a[i++] + 5?
With compound assignment, the left side (a[i++]) is evaluated only once, so i is incremented once. With the expanded form, i++ appears twice, causing undefined behavior because the order of evaluation is not guaranteed.
Q4: What is the associativity of assignment operators and why does it matter?
Assignment operators associate right-to-left. This allows chained assignments like a = b = c = 0, which is evaluated as a = (b = (c = 0)). First c gets 0, then b gets the result of that assignment (0), then a gets the result of that assignment (0).
Q5: Can you use compound assignment with pointers?
Yes, but only += and -= make sense with pointers. ptr += 3 advances the pointer by 3 elements (not 3 bytes). Other compound operators like *= don't work with pointers.
Summary
- The simple assignment
=stores a value; compound operators combine operation + assignment - Compound operators (
+=,-=,*=, etc.) are concise and evaluate the left side only once - Assignment has very low precedence and right-to-left associativity
x *= a + bmeansx = x * (a + b)— the whole right side is the operand- Never confuse
=(assignment) with==(comparison) — this is a classic C bug - Type conversion happens automatically during assignment (may truncate or lose precision)
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Assignment Operators in C Programming.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, operators, assignment, assignment operators in c programming
Related C Programming Topics