What Are Operators?
Operators are symbols that tell the compiler to perform specific mathematical, relational, or logical operations. C has one of the richest sets of operators among programming languages, giving you fine-grained control over data manipulation.
Arithmetic Operators
The basic math operators work on numeric types:
c
Critical Pitfall: Integer division in C always truncates toward zero.17 / 5gives3, not3.4. If you need decimal results, cast one operand todouble:c
Increment and Decrement
c
Warning: Never use increment/decrement twice on the same variable in one expression (e.g.,i++ + ++i). This is undefined behavior in C.
Relational Operators
These compare two values and return
1 (true) or 0 (false):c
Common Bug: Using=(assignment) instead of==(comparison) inside conditions:c
Logical Operators
Logical operators combine boolean expressions:
c
Short-Circuit Evaluation
C evaluates logical expressions left to right and stops early when the result is already determined:
c
Bitwise Operators
These operate on individual bits — essential for systems programming, embedded development, and performance-critical code.
| Operator | Name | Example |
|---|---|---|
& | AND | 0b1100 & 0b1010 = 0b1000 |
| | OR | 0b1100 | 0b1010 = 0b1110 |
^ | XOR | 0b1100 ^ 0b1010 = 0b0110 |
~ | NOT | ~0b1100 = 0b0011 (inverted) |
<< | Left shift | 0b0001 << 3 = 0b1000 |
>> | Right shift | 0b1000 >> 2 = 0b0010 |
c
Practical Use: Setting and Checking Flags
c
Assignment Operators
C provides compound assignment operators for concise code:
c
The Ternary Operator
The ternary operator
? : is a compact inline if-else:c
The Comma Operator
The comma operator evaluates both expressions but returns the value of the right one:
c
It's most commonly seen in
for loops with multiple variables:c
The sizeof Operator
While technically an operator (not a function),
sizeof returns the byte size of a type or expression:c
Operator Precedence (Simplified)
From highest to lowest priority:
| Priority | Operators | Associativity |
|---|---|---|
| 1 | () [] -> . | Left to right |
| 2 | ! ~ ++ -- (type) * & sizeof | 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 | ?: | Right to left |
| 11 | = += -= etc. | Right to left |
Tip: When in doubt, use parentheses!(a + b) * cis always clearer than relying on precedence rules.
Summary
C's operators give you power at every level — from simple arithmetic to direct bit manipulation. Master the precedence table, watch out for integer division and assignment-vs-comparison bugs, and you'll write cleaner, safer code.