Notifications

No notifications

/Phase 1

Operators in C

Doing Things with Data ⚙️

Operators are special symbols that perform operations on values. C has more operator categories than almost any other beginner language — that's part of why it's so powerful.

1. Arithmetic Operators

OpMeaningExampleResult
+Add5 + 38
-Subtract5 - 32
*Multiply5 * 315
/Divide10 / 33 (integer!)
%Remainder (modulo)10 % 31

> ⚠️ / between two ints does integer division — drops the decimal.

2. Assignment & Compound Assignment

int x = 5;     // basic assignment
x += 3;        // x = x + 3 → 8
x -= 1;        // x = x - 1 → 7
x *= 2;        // x = x * 2 → 14
x /= 7;        // x = x / 7 → 2
x %= 2;        // x = x % 2 → 0

3. Increment / Decrement

int i = 5;
i++;     // post-increment: use i, THEN add 1
++i;     // pre-increment:  add 1, THEN use i
i--;     // post-decrement
--i;     // pre-decrement

4. Comparison (Relational) Operators

OpMeaning
==Equal to
!=Not equal
<Less than
>Greater than
<=Less than or equal
>=Greater than or equal

> ⚠️ = is assignment, == is comparison. Mixing them is the #1 C bug.

5. Logical Operators

OpMeaning
&&AND (both true)
OR (at least one true)
!NOT (flip true ↔ false)

In C, 0 = false and anything else = true.

6. Bitwise Operators (preview)

&

^ ~ << >> — operate on individual bits. Covered fully in Phase 4.

On this page

Detailed Theory

Operators in C are not just convenience syntax — they map almost directly to single CPU instructions. That's why C feels "fast": when you write a + b, the compiler often emits exactly one add instruction.

Operator Precedence — Who Goes First?

Just like math, C has rules about which operator runs first.

int x = 2 + 3 * 4;   // 14, NOT 20 — * binds tighter than +

Selected precedence (high → low):

TierOperators
1() [] -> .
2! ~ ++ -- (unary)
3* / %
4+ -
5<< >>
6< <= > >=
7== !=
8&
9^
10
11&&
12
13?: (ternary)
14= += -=

> Don't memorise this table. When in doubt, use parentheses. Clear code beats clever code.

Integer vs Floating-Point Division

printf("%d\n", 7 / 2);          // 3
printf("%f\n", 7.0 / 2);        // 3.500000
printf("%f\n", (double)7 / 2);  // 3.500000

If either operand is floating-point, C promotes the whole expression to floating-point. If both are int, you get integer division.

Modulo (%)

The remainder operator. Used constantly in real code:

n % 2 == 0          // is n even?
n % 10              // last digit of n
i = (i + 1) % size; // circular increment (wrap around)

> % only works on integer types. 5.5 % 2 is a compile error. Use fmod() from for floats.

Pre vs Post Increment — The Real Difference

int i = 5;
int a = i++;   // a = 5, then i becomes 6   (post: use, then change)
int b = ++i;   // i becomes 7, then b = 7   (pre:  change, then use)

In a simple statement like i++; alone, there's no difference. The difference only matters when the value is being read in the same expression.

> Modern advice: prefer ++i in loops; it's marginally faster on objects (matters in C++) and never wrong in C.

The = vs == Trap

int x = 5;
if (x = 10) {     // ❌ assigns 10 to x, condition is "10" (true)
    // always runs!
}
if (x == 10) {    // ✅ comparison
    // runs only if x equals 10
}

Defensive trick — put the constant on the left:

if (10 == x)   // typo "10 = x" is a compile error → safer

Modern compilers warn about if (x = 10) if you enable -Wall.

Short-Circuit Evaluation

&& and || are lazy — they stop as soon as the answer is known.

if (p != NULL && *p == 5) { ... }   // *p only evaluated if p is not NULL — safe!
if (denominator != 0 && (numerator / denominator) > 0) { ... }

This is one of the most useful patterns in all of C — it lets you guard dangerous operations.

The Ternary Operator ?:

int max = (a > b) ? a : b;
//        condition  ↑    ↑
//                  if-true if-false

Equivalent to:

int max;
if (a > b) max = a;
else       max = b;

Use ternary for simple value-choice. Don't nest more than one level — it gets unreadable fast.

Operator Pitfalls Summary

BugWhy it happens
if (x = 5)= instead of ==
5 / 2 = 2 not 2.5integer division
fabs(a-b) < EPS instead of a==bfloat equality is unreliable
Negative % resultssign of % follows dividend in C99+
Mixing signed/unsignedimplicit conversion bugs

Master operators and precedence and you've absorbed about 30 % of the entire C language.