Introduction & Syntax
Operators and Expressions · Study material
Operators in C combine values into expressions: a + b, i++, x > 5 && y < 10, n += 3. Every C program is built from them, and placement tests love the details – integer division, the sign of %, pre- vs post-increment, precedence, the comma operator and expressions such as i = i++ that have no defined result at all. This page explains each group of operators and the rules that decide how an expression is evaluated, then gives you 40 practice questions checked on a real compiler.
On this page
- Arithmetic operators, integer division and
% - Increment and decrement: prefix vs postfix
- Relational, logical, assignment, comma, conditional and
sizeof - Precedence, associativity – and undefined unsequenced expressions
Arithmetic operators
+ - * / % work as expected, with two C rules to remember: dividing two integers truncates toward zero (17 / 5 is 3, -7 / 2 is −3), and a % b has the sign of a. % works only on integers; use fmod() for doubles.
#include <stdio.h>
int main(void) {
int a = 17, b = 5;
printf("%d %d %d %d\n", a + b, a - b, a * b, a / b); /* int / int truncates */
printf("%d %d %.2f\n", a % b, -a % b, (double) a / b); /* % keeps the sign of a */
return 0;
}
22 12 85 3 2 -2 3.40
Increment and decrement
++ and -- add or subtract 1. The prefix form (++i) changes the variable and gives the new value; the postfix form (i++) gives the old value and changes the variable afterwards.
#include <stdio.h>
int main(void) {
int i = 5;
int x = i++; /* use the old value (5), then increment: i = 6 */
int y = ++i; /* increment first (i = 7), then use it */
printf("x=%d y=%d i=%d\n", x, y, i);
return 0;
}
x=5 y=7 i=7
Precedence and associativity
Precedence decides which operator binds first (* before +); associativity decides the order among operators of the same level – left to right for most binary operators, right to left for assignment, unary operators and ?:.
#include <stdio.h>
int main(void) {
int r1 = 2 + 3 * 4; /* * before + */
int r2 = (2 + 3) * 4; /* brackets first */
int r3 = 20 / 4 / 2; /* left to right: (20/4)/2 */
int a, b, c;
a = b = c = 7; /* right to left: a = (b = (c = 7)) */
printf("%d %d %d %d\n", r1, r2, r3, a + b + c);
return 0;
}
14 20 2 21
| Level (high → low) | Operators | Associativity |
|---|---|---|
| Postfix | () [] -> . x++ x-- | left to right |
| Unary | ++x --x + - ! ~ * & (type) sizeof | right to left |
| Multiplicative | * / % | left to right |
| Additive | + - | left to right |
| Shift | << >> | left to right |
| Relational | < <= > >= | left to right |
| Equality | == != | left to right |
| Bitwise | & then ^ then | | left to right |
| Logical | && then || | left to right |
| Conditional | ?: | right to left |
| Assignment | = += -= *= /= %= … | right to left |
| Comma | , | left to right |
Relational and logical operators
Comparisons (< > == != …) and logical operators (&& || !) produce the int 1 or 0. && and || evaluate left to right and stop as soon as the result is known (short-circuit).
#include <stdio.h>
int main(void) {
int x = 7;
/* comparisons and logical operators give the int 1 (true) or 0 (false) */
printf("%d %d %d %d\n", x > 5, x == 5, !x, (x > 5) + (x < 10));
return 0;
}
1 0 0 2
Assignment, compound assignment and the comma operator
n += 5 means n = n + 5 (with n evaluated once). The comma operator evaluates its left operand, discards it, and yields its right operand – mostly seen in for loops. Not every comma is this operator: the commas between function arguments and in declarations are just separators.
#include <stdio.h>
int main(void) {
int n = 10;
n += 5; /* 15 */
n *= 2; /* 30 */
n -= 4; /* 26 */
n /= 3; /* 8 */
n %= 5; /* 3 */
int k = (n++, n * 10); /* comma: do n++, then the value is n * 10 */
printf("%d %d\n", n, k);
return 0;
}
4 40
Unsequenced side effects: undefined behaviour
If one expression modifies a variable twice, or modifies it and reads it elsewhere without a sequence point in between, the behaviour is undefined: i = i++, a[i] = i++, printf("%d %d", i++, i++). Different compilers print different results, and the optimiser may do anything. Split such expressions into separate statements.
i = i++; /* undefined: i modified twice */
a[i] = i++; /* undefined: i read and modified, unsequenced */
printf("%d %d\n", i++, i++);/* undefined: two unsequenced modifications */
x = i++ + i++; /* undefined */
i++; /* fine: one change per statement */
a[i] = i;
Key points to remember
- int / int truncates toward zero;
%takes the sign of the left operand. i++yields the old value,++ithe new one.- When in doubt, add brackets – they cost nothing and remove all doubt.
- Relational and logical operators return the int 0 or 1.
- Never modify a variable twice in one expression.
Frequently asked questions
What is the difference between ++i and i++?
Both add 1 to i. As an expression, ++i has the new value and i++ the old value. On its own line (i++;) there is no difference.
Why does 5 / 2 give 2 in C?
Both operands are integers, so the division is integer division and the fraction is discarded. Write 5 / 2.0 or (double) 5 / 2 for 2.5.
What is a sequence point?
A point where all earlier side effects are complete – for example at the end of a full expression, after the left operand of &&, ||, ?: and the comma operator, and before a function is called.
Where can I learn more?
Read Operators in C and Type Conversion in C, then practise with the exercises below.