C Operators
C provides rich operators, divided into arithmetic, relational, logical, bitwise, assignment, conditional, pointer, member, and comma operators.
1. Arithmetic Operators
+ - * / % ++ --
Note: Integer division truncates, be careful with /% and negative numbers; evaluation order of ++i vs i++.
2. Relational and Comparison
== != > < >= <=
3. Logical Operators
&& || !, short-circuit evaluation.
4. Bitwise Operators
& | ^ ~ << >>
- Commonly used for flag bits, masks, performance optimization
5. Assignment and Compound Assignment
= += -= *= /= %= <<= >>= &= |= ^=
6. Conditional Operator
cond ? a : b
7. Pointer and Member Operators
* & -> . []
8. sizeof and Alignment
sizeof(expr) or sizeof(type)
9. Operator Precedence and Associativity
- Multiplication before addition
- Use parentheses to improve readability, avoid relying on complex precedence
10. Example
c
#include <stdio.h>
int main(void) {
unsigned x = 0b1010u; // when compiler supports binary literals
unsigned y = 0b1100u;
printf("x|y=%u, x&y=%u, x^y=%u\n", x|y, x&y, x^y);
return 0;
}11. Summary
Familiarity with operator semantics and precedence is the foundation for writing correct expressions; use parentheses to make intentions clearer.