Operators in C: Types of Operators
What is an Operator?
An operator is a symbol that tells the compiler to perform a specific mathematical, relational, or logical operation.
Types of Operators in C
- Arithmetic operators —
+ - * / % - Relational operators —
== != > < >= <= - Logical operators —
&& || ! - Bitwise operators —
& | ^ ~ << >> - Assignment operators —
= += -= *= /= - Increment/decrement operators —
++ -- - Conditional (ternary) operator —
?: - Special operators —
sizeof, comma operator, pointer operators* &
Understanding operator precedence and associativity is essential for writing correct expressions.
Operators in Action: One Program, Many Results
Seeing operators work on real values clears up most confusion. Notice especially how integer division and the modulo operator behave.
#include <stdio.h>
int main(void) {
int a = 17, b = 5;
printf("%d\n", a / b); /* 3 : integer division drops the fraction */
printf("%d\n", a % b); /* 2 : remainder */
printf("%.2f\n", a / 2.0); /* 8.50 : one double operand gives a double result */
printf("%d\n", a > b); /* 1 : relational results are 1 (true) or 0 (false) */
printf("%d\n", a & b); /* 1 : 10001 AND 00101 = 00001 */
printf("%d\n", b << 1); /* 10 : shifting left by one doubles the value */
int max = (a > b) ? a : b; /* ternary picks the larger value */
printf("%d\n", max); /* 17 */
return 0;
}
Pre-increment vs Post-increment
Both ++x and x++ add one to x. The difference is the value the expression gives back. Pre-increment changes x first and returns the new value. Post-increment returns the old value and then changes x.
int x = 5;
int y = x++; /* y = 5, x = 6 */
int z = ++x; /* x = 7, z = 7 */
On a line by itself, such as the update part of a for loop, the two forms behave the same. The difference only shows up when the result is used inside a larger expression.
How Precedence Decides the Answer
When an expression mixes operators, C evaluates higher-precedence operators first. Operators at the same level follow associativity, which is left to right for most groups and right to left for unary, ternary and assignment operators.
| Level (high to low) | Operators |
|---|---|
| Postfix | () [] -> . and postfix ++ -- |
| Unary | ! ~ ++ -- sizeof, unary - * &, casts |
| Multiplicative | * / % |
| Additive | + - |
| Shift | << >> |
| Relational and equality | < <= > >=, then == != |
| Bitwise | &, then ^, then | |
| Logical | &&, then || |
| Conditional, assignment, comma | ?:, = += -= and friends, then , |
So 2 + 3 * 4 is 14, not 20. A well-known trap is if (flags & MASK == 0): equality binds tighter than bitwise AND, so it is read as flags & (MASK == 0). Add parentheses whenever bitwise and comparison operators meet.
Mistakes Worth Avoiding
- Using
=instead of==in a condition.if (x = 0)assigns zero and the condition is always false. Compilers warn about this when warnings are enabled. - Modifying a variable twice in one expression. Code like
i = i++ + ++i;is undefined behaviour; different compilers can give different results. - Forgetting short-circuit evaluation. In
a && b, b is never evaluated if a is false. This is useful for guards likep != NULL && p->value > 0, but surprising if b had a side effect you relied on. - Applying
%to floating-point values. It only works on integers; usefmod()frommath.hfor doubles.
Practical habit: compile with warnings on (for example gcc -Wall -Wextra) and use parentheses generously. Clear code beats clever code.
Ready to master real-world C Programming development?
Learn C Programming hands-on with mentor-led, live sessions.