C Operators
Challenge Gallery
Quick Reference
Operator precedence (high to low):
| Precedence | Operators | Notes |
|---|---|---|
| Highest | () [] -> . |
Grouping, access |
| Unary | ! ~ ++ -- * & (type) |
Right-to-left |
| Arithmetic | * / % then + - |
Left-to-right |
| Comparison | < <= > >= then == != |
|
| Bitwise | & ^ \| |
|
| Logical | && then \|\| |
Short-circuit |
| Ternary | ? : |
Right-to-left |
| Assignment | = += -= etc. |
Right-to-left |
Common Pitfalls
- = vs == –
if (x = 5)assigns 5 to x (always true). Useif (x == 5)to compare. - & vs && –
&is bitwise AND.&&is logical AND. Mixing them up causes subtle bugs. - Integer division –
5 / 2is2, not2.5. Cast one operand:(double) 5 / 2. - Truncation on cast –
(int) 3.99is3, not4. Casting truncates, it does not round.