Master arithmetic, logical, bitwise, and assignment operators. Understand precedence, avoid undefined behavior, and write safe, predictable expressions that compile correctly and run reliably.
The foundation of computational expressions. Perform mathematical calculations on numerical values.
Adds two numbers together.
Subtracts second number from first.
Multiplies two numbers.
Divides first by second. Integer division truncates.
Returns remainder. Integers only.
Increases value by 1 (Pre or Post).
Compare two values and return boolean result: 1 (true) or 0 (false).
| Operator | Name | Example | Result |
|---|---|---|---|
| == | Equal to | 5 == 5 | 1 (True) |
| != | Not Equal | 5 != 3 | 1 (True) |
| > | Greater | 10 > 5 | 1 (True) |
| < | Less | 3 < 8 | 1 (True) |
| >= | Greater/Equal | 5 >= 5 | 1 (True) |
== (comparison) with = (assignment). Using if (x = 5) assigns 5 to x instead of checking equality. Always use == for comparisons!
Combine multiple boolean conditions.
Returns true only if BOTH conditions are true.
Returns true if AT LEAST ONE condition is true.
Inverts the boolean value (True becomes False).
Low-level operations on individual bits. Essential for flags and optimization.
Bits are 1 if both are 1.
Bits are 1 if either is 1.
Bits are 1 if different.
Inverts all bits (0->1, 1->0).
Shifts bits left (Multiply by 2^n).
Shifts bits right (Divide by 2^n).
Shorthand syntax for modifying variables.
| Operator | Example | Equivalent To |
|---|---|---|
| = | x = 10 | Assign 10 to x |
| += | x += 5 | x = x + 5 |
| -= | x -= 3 | x = x - 3 |
| *= | x *= 2 | x = x * 2 |
| /= | x /= 2 | x = x / 2 |
| %= | x %= 3 | x = x % 3 |
| &= | x &= 5 | x = x & 5 |
| |= | x |= 5 | x = x | 5 |
| ^= | x ^= 5 | x = x ^ 5 |
Order of operations. When in doubt, use parentheses ().
| Level | Operators | Associativity |
|---|---|---|
| 1 | () [] . -> | Left to Right |
| 2 | ! ~ ++ -- + - * & (Unary) | Right to Left |
| 3 | * / % | Left to Right |
| 4 | + - | Left to Right |
| 5 | << >> | Left to Right |
| 6 | < <= > >= | Left to Right |
| 7 | == != | Left to Right |
| 8 | & | Left to Right |
| 9 | ^ | Left to Right |
| 10 | | | Left to Right |
| 11 | && | Left to Right |
| 12 | || | Left to Right |
| 13 | ?: (Ternary) | Right to Left |
| 14 | = += -= etc. | Right to Left |
| 15 | , | Left to Right |
Writing valid C means avoiding ambiguous operations that confuse the compiler.
A && B, if A is false, B is never evaluated.A || B, if A is true, B is never evaluated.
i = i++; or arr[i] = i++;