Last 30 Days
No notifications
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.
| Op | Meaning | Example | Result |
+ | Add | 5 + 3 | 8 |
- | Subtract | 5 - 3 | 2 |
* | Multiply | 5 * 3 | 15 |
/ | Divide | 10 / 3 | 3 (integer!) |
% | Remainder (modulo) | 10 % 3 | 1 |
> ⚠️ / between two ints does integer division — drops the decimal.
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 → 0int i = 5;
i++; // post-increment: use i, THEN add 1
++i; // pre-increment: add 1, THEN use i
i--; // post-decrement
--i; // pre-decrement| Op | Meaning | |||
== | Equal to | |||
!= | Not equal | |||
< | Less than | |||
> | Greater than | |||
<= | Less than or equal | |||
>= | Greater than or equal | > ⚠️ 5. Logical Operators | Op | Meaning |
&& | AND (both true) | |||
| OR (at least one true) | |||
! | NOT (flip true ↔ false) | In C, 0 = false and anything else = true. 6. Bitwise Operators (preview)
|
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.
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):
| Tier | Operators | |
| 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.
printf("%d\n", 7 / 2); // 3
printf("%f\n", 7.0 / 2); // 3.500000
printf("%f\n", (double)7 / 2); // 3.500000If either operand is floating-point, C promotes the whole expression to floating-point. If both are int, you get integer division.
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.
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.
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 → saferModern compilers warn about if (x = 10) if you enable -Wall.
&& 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.
int max = (a > b) ? a : b;
// condition ↑ ↑
// if-true if-falseEquivalent 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.
| Bug | Why it happens |
if (x = 5) | = instead of == |
5 / 2 = 2 not 2.5 | integer division |
fabs(a-b) < EPS instead of a==b | float equality is unreliable |
Negative % results | sign of % follows dividend in C99+ |
| Mixing signed/unsigned | implicit conversion bugs |
Master operators and precedence and you've absorbed about 30 % of the entire C language.