A structured lesson workspace with readable content, hands-on examples, and a clean path to completion.
The simple assignment = stores a value. The compound assignments += -= *= /= %= <<= >>= &= |= ^= combine an operation with assignment. x += 5 is equivalent to x = x + 5 with one important nuance: x is evaluated once. If x is an expression with side effects, such as *ptr++ or arr[compute_index()], the compound form evaluates it once and the expanded form evaluates it twice. This matters for correctness, not just brevity.
#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 %= 5; printf("x %%= 5: %d\n", x);int a = 12;a &= 10; printf("a &= 10: %d\n", a);a |= 5; printf("a |= 5: %d\n", a);a ^= 3; printf("a ^= 3: %d\n", a);a <<= 2; printf("a <<= 2: %d\n", a);a >>= 1; printf("a >>= 1: %d\n", a);return 0;}
Assignment is right-associative. a = b = c = 0 parses as a = (b = (c = 0)). The value of an assignment expression is the assigned value. This lets you initialize multiple variables in one statement. It also lets you write bugs like if (x = 0) when you meant if (x == 0), which we covered in the if statements lesson.
#include <stdio.h>int main(void) {int a, b, c;a = b = c = 42;printf("a=%d, b=%d, c=%d\n", a, b, c);return 0;}
Compound assignment operators are syntactic sugar with a semantic guarantee: the left operand is evaluated once. Use them for clarity and correctness. Next: bitwise operators, where you manipulate individual bits for flags, masks, and low-level protocol work.
Finish the lesson once you have worked through the material. This awards ★ 20 XP.