Learn C
#include "operators/op_assignment.h"

Assignment Operators in C

#concept

Plain = stores a value, but assignment is an expression in C — it yields the assigned value, which is why chains like a = b = 0 work (right-associative) and why = inside a condition compiles (usually a bug). The compound forms (+=, -=, *=, /=, %=, plus bitwise &=, |=, ^=, <<=, >>=) fold an operation into the store.

Assignment requires a modifiable lvalue on the left — array names, constants, and function results cannot be assigned to. Chained and embedded assignments trade brevity for review friction; most style guides restrict = to standalone statements.

#how it works
1

= is an expression

a = b = 0 assigns right to left evaluating to 0 — useful for initialization chains, dangerous inside if conditions.

2

Compound operators

x += 3 means x = x + 3. Every arithmetic and bitwise operator has a compound form.

3

Left must be assignable

Only modifiable lvalues accept = — variables and dereferenced pointers qualify; literals, array names, and consts do not.

4

Keep = out of conditions

if ((n = read())) uses deliberate assignment-in-condition with doubled parentheses; bare if (x = 5) is virtually always a bug.

#example
#include <stdio.h>
 
int main(void) {
int a = 0, b = 0;
a = b = 10;
a += 5;
b *= 3;
printf("%d, %d\n", a, b);
return 0;
}
Output
15, 30
1a = b = 10 chains right to left: b becomes 10, then a becomes 10.
2a += 5 lifts a to 15; b *= 3 lifts b to 30.
3Both print as computed from the two compound updates.
#use cases
Zeroing several variables at once with a = b = c = 0
Accumulating loop results with += instead of full reassignments
Shifting and masking hardware registers with <<=, &=, |= in firmware
Flagging accidental assignment-in-condition with -Wall -Werror

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C yourself — building beats watching.