C provides a rich set of operators for arithmetic, comparison, logic, and more. But operators become genuinely useful only once you understand how they combine into expressions, and crucially, in what order they’re evaluated when several appear together. This is Post 4 of the Unit I series.
Categories of Operators
1. Arithmetic Operators
+ addition
- subtraction
* multiplication
/ division
% modulus (remainder) — only works on integers
2. Relational Operators
== equal to
!= not equal to
> greater than
< less than
>= greater than or equal to
<= less than or equal to
3. Logical Operators
&& logical AND
|| logical OR
! logical NOT
4. Assignment Operators
= assign
+= add and assign (a += 5 is short for a = a + 5)
-= subtract and assign
*= multiply and assign
/= divide and assign
%= modulus and assign
5. Increment/Decrement Operators
++ increment by 1 (can be prefix ++a or postfix a++)
-- decrement by 1
Example: Prefix vs. Postfix
#include <stdio.h>
int main() {
int a = 5, b = 5;
printf("Postfix: %d\n", a++); // prints 5 (uses value FIRST, then increments)
printf("After postfix, a = %d\n", a); // now 6
printf("Prefix: %d\n", ++b); // increments FIRST, then prints 6
printf("After prefix, b = %d\n", b);
return 0;
}
Sample Output
Postfix: 5
After postfix, a = 6
Prefix: 6
After prefix, b = 6
Postfix vs. prefix: a++ evaluates to a’s current value, and THEN increments it. ++a increments a FIRST, and evaluates to the new value. When used as a standalone statement (just a++; on its own line), the difference doesn’t matter — it only matters when the expression’s value is also being used immediately, like inside a printf() call.
Expressions
An expression is any valid combination of operators, constants, and variables that evaluates to a single value.
a + b * c // an expression
(x > 0) && (y > 0) // an expression that evaluates to true/false (1 or 0 in C)
Precedence and Associativity
When an expression has multiple operators, C needs rules to decide which operation happens first.
- Precedence determines which operator binds “tighter” — e.g.
*and/have higher precedence than+and-, so they’re evaluated first, just like in ordinary arithmetic (BODMAS/PEMDAS). - Associativity determines the order of evaluation when operators of the same precedence appear together — left-to-right or right-to-left, depending on the operator.
A Simplified Precedence Table (Highest to Lowest)
| Operators | Associativity |
|---|---|
| () [] (function call, array index) | Left to right |
| ++ — (postfix), ! (unary) | Right to left |
| * / % | Left to right |
| + – (binary) | Left to right |
| < <= > >= | Left to right |
| == != | Left to right |
| && | Left to right |
| || | Left to right |
| = += -= *= /= | Right to left |
Worked Example
int result = 10 + 20 * 3 - 4 / 2;
Step by step:
1. 20 * 3 = 60 (* has higher precedence than + and -)
2. 4 / 2 = 2 (/ also has higher precedence)
3. 10 + 60 = 70 (now left-to-right for + and -)
4. 70 - 2 = 68
result = 68
When in doubt, use parentheses: Even when you know the precedence rules, adding explicit parentheses like (20 * 3) – (4 / 2) + 10 makes the intended order obvious to anyone reading the code later — including you, months from now.
Expression Evaluation with Mixed Types
#include <stdio.h>
int main() {
int a = 7, b = 2;
float c = 7.0, d = 2.0;
printf("int / int = %d\n", a / b); // 3 (integer division truncates)
printf("float / float = %.2f\n", c / d); // 3.50
printf("int / float = %.2f\n", a / d); // 3.50 (a is promoted to float)
printf("mod (int only) = %d\n", a % b); // 1
return 0;
}
Sample Output
int / int = 3
float / float = 3.50
int / float = 3.50
mod (int only) = 1
Type Conversion
Type conversion happens in two ways:
- Implicit conversion (also called “type promotion”) — the compiler automatically converts one operand’s type to match the other, following C’s promotion rules (in general:
char→int→float→double, promoting toward the “wider” type). This is what happened witha / dabove —a(int) was implicitly promoted tofloatbecausedis afloat. - Explicit conversion (type casting) — you manually force a conversion using
(type):float avg = (float) totalMarks / numSubjects;
A classic bug this explains: int total = 7, count = 2; float avg = total / count; gives avg = 3.00, NOT 3.50 — because total / count is computed as INTEGER division first (7/2 = 3, truncated), and only the final int result (3) is converted to float for storage. Casting one operand BEFORE the division — (float) total / count — is what actually fixes this.
Summary
- C has arithmetic, relational, logical, assignment, and increment/decrement operators.
- Precedence decides which operator is applied first; associativity breaks ties between operators of equal precedence.
- Prefix (
++a) increments before using the value; postfix (a++) uses the value before incrementing. - Implicit conversion happens automatically per C’s promotion rules; explicit conversion (casting) is done manually with
(type). - Integer division truncates — cast an operand to
float/doublebefore the division if you need a fractional result.
Further Reading
- Operator Precedence and Associativity in C Programming (video embedded above)
- GeeksforGeeks: Operator Precedence and Associativity in C
Next in this series: decision making with if, if-else, and switch statements.