Module 2 · Variables, Types, Input and Operators — topic 5 of 6
Operators
C's operators — arithmetic, relational, logical, assignment, increment/decrement, conditional, comma and sizeof — and the precedence and associativity rules that decide how expressions are evaluated.
Learning objectives
- Use arithmetic, relational, logical and assignment operators correctly
- Predict results of integer division, modulus and increment operators
- Apply precedence and associativity, and recognise undefined evaluation orders
Definition
An operator is a symbol that performs an operation on one or more operands to produce a value. Precedence decides which operator binds first in an expression, and associativity decides the order for operators of equal precedence.
Introduction
Operators are the verbs of a program. Most look familiar from mathematics, but C has its own rules: dividing two integers throws away the remainder, = assigns rather than compares, and && may not evaluate its right side at all.
This lesson covers every everyday operator. Bitwise operators (&, |, ^, <<, >>, ~) get a dedicated lesson later.
Why it matters
Output-prediction questions in exams and interviews are mostly about operators: integer division, % with negative numbers, i++ versus ++i, and precedence. Getting these right is essential.
Short-circuit evaluation is not just trivia — it is how C programs safely write if (p != NULL && p->value > 0).
Detailed explanation
Arithmetic: + - * / %. With two integers, / truncates toward zero (7 / 2 is 3, -7 / 2 is −3) and % gives a remainder with the sign of the left operand (-7 % 2 is −1). % works only on integers.
Relational: < <= > >= == != produce int 1 (true) or 0 (false). Logical: &&, ||, !. && and || short-circuit: if the left side decides the result, the right side is not evaluated.
Assignment: = and compound forms += -= *= /= %=. An assignment is itself an expression whose value is the stored value, which is why a = b = 0; works.
Increment/decrement: ++x increments then yields the new value; x++ yields the old value then increments. Modifying the same variable twice without a sequence point, e.g. i = i++ + ++i;, is undefined behaviour.
Conditional: cond ? a : b. Comma: a, b evaluates a, then yields b. sizeof yields a size in bytes.
Syntax
- Use parentheses whenever precedence is not obvious to a reader.
==compares;=assigns.- Relational and logical operators yield 0 or 1 of type
int.
Code examples
Integer division and modulus
3 2 -3 -2 3.40
Division truncates toward zero; the remainder keeps the sign of the dividend. One double operand gives real division.
Pre- and post-increment
a=5 b=7 i=7
More examples
Short-circuiting protects a division
skipped division safely result 0 after 1 call(s) odd 1
Step-by-step explanation
d != 0is false, so&&never evaluatesn / d— the division by zero never happens.is_positive(-1)returns 0, so the second call is skipped;callsis 1.x % 2 == 0 ? "even" : "odd":%binds tighter than==, which binds tighter than?:, so 7 % 2 is 1, the comparison is false, and "odd" is chosen.2 + 3 * 4 > 10 && 1groups as((2 + (3 * 4)) > 10) && 1→14 > 10is 1 →1 && 1is 1.
Expected output
Running “Short-circuiting protects a division” prints exactly:
Practical applications
%checks divisibility, extracts digits (n % 10) and wraps array indexes in circular buffers.- Short-circuit checks guard against null pointers in almost every C codebase.
- The conditional operator keeps simple choices like
max = a > b ? a : bon one line.
Best practices
- Add parentheses to make precedence explicit in mixed expressions.
- Never modify a variable more than once in one expression.
- Write constant-first comparisons (
if (100 == marks)) only if your team uses that style; otherwise rely on-Wall.
Common mistakes
Assignment instead of comparison
Avoid
Prefer
marks = 100 stores 100 and evaluates to 100 (true), so the message always prints and the data is destroyed.
Expecting decimals from int division
Avoid
Prefer
sum / count is computed in integers first; converting the truncated result to double is too late.
Chained comparisons
Avoid
Prefer
0 < x < 10 parses as (0 < x) < 10, which compares 0 or 1 with 10 and is always true.
Important notes
- Before C99, the sign of
%with negative operands was implementation-defined; C99 and later require truncation toward zero. - The order in which function arguments are evaluated is unspecified:
printf("%d %d", i++, i++);is undefined behaviour.
Key takeaways
- Integer
/truncates toward zero;%keeps the sign of the left operand. &&and||short-circuit; relational results are 0 or 1.x++yields the old value,++xthe new one; don't modify a variable twice in one expression.
Try it yourself
- 1Print the last digit and the number without its last digit for 4729.
Show hint
Use
% 10and/ 10. - 2Predict the output of
int a = 5; printf("%d\n", a++ * 2 + --a);— then explain why the correct answer is “undefined behaviour”.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
15 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?
- BeginnerWhat is printed?
- BeginnerWhat is printed?