C Operators
Operators are the symbols that perform work on values: adding numbers, comparing them, combining conditions. The values they work on are called operands. In price * 2, the operator is * and the operands are price and 2.
C groups its operators into a few families:
- Arithmetic operators
- Assignment operators
- Comparison (relational) operators
- Logical operators
- Bitwise operators (covered in C Bitwise Operators)
- A few special ones such as
sizeofand the conditional?:
Arithmetic operators
| Operator | Name | Example | Result |
|---|---|---|---|
+ |
Addition | 7 + 3 |
10 |
- |
Subtraction | 7 - 3 |
4 |
* |
Multiplication | 7 * 3 |
21 |
/ |
Division | 7 / 3 |
2 (integers) |
% |
Remainder (modulo) | 7 % 3 |
1 |
++ |
Increment | x++ |
adds 1 to x |
-- |
Decrement | x-- |
subtracts 1 from x |
#include <stdio.h>
int main(void) {
int a = 17, b = 5;
printf("%d + %d = %d\n", a, b, a + b);
printf("%d - %d = %d\n", a, b, a - b);
printf("%d * %d = %d\n", a, b, a * b);
printf("%d / %d = %d\n", a, b, a / b);
printf("%d %% %d = %d\n", a, b, a % b);
printf("17.0 / 5 = %.1f\n", 17.0 / 5);
return 0;
}17 + 5 = 22
17 - 5 = 12
17 * 5 = 85
17 / 5 = 3
17 % 5 = 2
17.0 / 5 = 3.4
Dividing two integers gives an integer (the fraction is dropped). The % operator returns the remainder and only works on integers — it is handy for testing whether a number is even (n % 2 == 0) or for wrapping around (hour % 12).
Watch out: Dividing an integer by zero is undefined behaviour and usually crashes the program. Check the divisor first.
Increment and decrement
x++ and ++x both add 1 to x. The difference only matters when the expression's value is used: the postfix form x++ gives the old value, the prefix form ++x gives the new value.
#include <stdio.h>
int main(void) {
int x = 5;
int a = x++; // a gets 5, then x becomes 6
int y = 5;
int b = ++y; // y becomes 6, then b gets 6
printf("a=%d x=%d\n", a, x);
printf("b=%d y=%d\n", b, y);
return 0;
}a=5 x=6
b=6 y=6
Tip: Never modify the same variable twice in one expression (
i = i++ + ++i;). The result is undefined.
Assignment operators
| Operator | Example | Same as |
|---|---|---|
= |
x = 5 |
x = 5 |
+= |
x += 3 |
x = x + 3 |
-= |
x -= 3 |
x = x - 3 |
*= |
x *= 3 |
x = x * 3 |
/= |
x /= 3 |
x = x / 3 |
%= |
x %= 3 |
x = x % 3 |
&=, |=, ^=, <<=, >>= |
x <<= 1 |
bitwise versions |
Comparison operators
Comparisons return 1 for true and 0 for false (type int):
| Operator | Meaning | Example (a = 4, b = 9) |
|---|---|---|
== |
equal to | a == b → 0 |
!= |
not equal to | a != b → 1 |
> |
greater than | a > b → 0 |
< |
less than | a < b → 1 |
>= |
greater than or equal | a >= 4 → 1 |
<= |
less than or equal | b <= 8 → 0 |
Watch out:
=assigns,==compares.if (x = 5)assigns 5 toxand is always true — a classic bug. GCC warns about it with-Wall.
Logical operators
Logical operators combine conditions:
| Operator | Name | True when |
|---|---|---|
&& |
AND | both sides are true |
|| |
OR | at least one side is true |
! |
NOT | the operand is false |
#include <stdio.h>
int main(void) {
int age = 25;
int has_ticket = 1;
printf("adult: %d\n", age >= 18);
printf("adult with ticket: %d\n", age >= 18 && has_ticket);
printf("child or senior: %d\n", age < 12 || age >= 65);
printf("no ticket: %d\n", !has_ticket);
return 0;
}adult: 1
adult with ticket: 1
child or senior: 0
no ticket: 0
&& and || use short-circuit evaluation: if the left side already decides the result, the right side is never evaluated. That makes guards like count != 0 && total / count > 10 safe.
The conditional operator and sizeof
condition ? a : b evaluates to a if the condition is true, otherwise b. It is a compact if…else for picking values. sizeof gives the size of a type or variable in bytes.
#include <stdio.h>
int main(void) {
int n = 7;
int bigger = (n > 10) ? n : 10;
printf("%d is %s\n", n, (n % 2 == 0) ? "even" : "odd");
printf("bigger of n and 10: %d\n", bigger);
printf("sizeof n = %zu\n", sizeof n);
return 0;
}7 is odd
bigger of n and 10: 10
sizeof n = 4
Precedence
When an expression has several operators, precedence decides which runs first — just like multiplication before addition in maths. From higher to lower, the most common ones are:
| Level | Operators |
|---|---|
| 1 | () [] -> . postfix ++ -- |
| 2 | unary ! - + ++ --, * (dereference), & (address), sizeof, casts |
| 3 | * / % |
| 4 | + - |
| 5 | << >> |
| 6 | < <= > >= |
| 7 | == != |
| 8–10 | &, ^, | |
| 11–12 | &&, || |
| 13 | ?: |
| 14 | = += -= … |
So 2 + 3 * 4 is 14, not 20. When in doubt, add parentheses — they cost nothing and make intent clear.
Test yourself
What is the value of 19 % 4?
- 4
- 3
- 4.75
- 0
After int x = 3; int y = x++;, what are x and y?
- x=4, y=4
- x=4, y=3
- x=3, y=4
- x=3, y=3
What does 5 > 3 && 2 > 4 evaluate to?
- 1
- 0
- true
- It does not compile