Skip to content

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.

Beginner 18 min read·Lesson 9 of 37 in C Programming

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.

Precedence (high → low)OperatorsAssociativity
1() [] -> . postfix ++ --left to right
2unary + - ! ~ ++ -- * & sizeof castsright to left
3* / %left to right
4+ -left to right
5<< >>left to right
6< <= > >=left to right
7== !=left to right
8–10& then ^ then |left to right
11–12&& then ||left to right
13?:right to left
14= += -= …right to left
15,left to right

Syntax

C
q = a / b; /* integer division if both are int */
r = a % b; /* remainder */
ok = (x > 0) && (y > 0);
count += 2; /* count = count + 2 */
max = (a > b) ? a : b;
n = i++; /* n gets old i */
m = ++i; /* m gets new i */
  • 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

C
#include <stdio.h>
int main(void) {
printf("%d %d\n", 17 / 5, 17 % 5);
printf("%d %d\n", -17 / 5, -17 % 5);
printf("%.2f\n", 17 / 5.0);
return 0;
}
Output
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

C
#include <stdio.h>
int main(void) {
int i = 5;
int a = i++; /* a = 5, then i = 6 */
int b = ++i; /* i = 7, then b = 7 */
printf("a=%d b=%d i=%d\n", a, b, i);
return 0;
}
Output
a=5 b=7 i=7

More examples

Short-circuiting protects a division

C
#include <stdio.h>
int calls = 0;
int is_positive(int x) {
calls++;
return x > 0;
}
int main(void) {
int d = 0, n = 10;
if (d != 0 && n / d > 2)
printf("big ratio\n");
else
printf("skipped division safely\n");
int r = is_positive(-1) && is_positive(5);
printf("result %d after %d call(s)\n", r, calls);
int x = 7;
printf("%s\n", x % 2 == 0 ? "even" : "odd");
printf("%d\n", 2 + 3 * 4 > 10 && 1);
return 0;
}
Output
skipped division safely
result 0 after 1 call(s)
odd
1

Step-by-step explanation

  • d != 0 is false, so && never evaluates n / d — the division by zero never happens.
  • is_positive(-1) returns 0, so the second call is skipped; calls is 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 && 1 groups as ((2 + (3 * 4)) > 10) && 1 → 14 > 10 is 1 → 1 && 1 is 1.

Expected output

Running “Short-circuiting protects a division” prints exactly:

TextOutput
skipped division safely
result 0 after 1 call(s)
odd
1

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 : b on 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

if (marks = 100)
printf("Full marks\n");

Prefer

if (marks == 100)
printf("Full marks\n");

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

double avg = sum / count;

Prefer

double avg = (double)sum / count;

sum / count is computed in integers first; converting the truncated result to double is too late.

Chained comparisons

Avoid

if (0 < x < 10) { /* ... */ }

Prefer

if (0 < x && x < 10) { /* ... */ }

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, ++x the new one; don't modify a variable twice in one expression.

Try it yourself

  1. 1
    Print the last digit and the number without its last digit for 4729.
    Show hint

    Use % 10 and / 10.

  2. 2
    Predict 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?
Practise this topic