An operator is a symbol that performs an operation on values (called operands). a + b is an expression: + is the operator, a and b are operands.
Arithmetic operators#
| Operator | Meaning | Example (a = 17, b = 5) |
Result |
|---|---|---|---|
+ |
addition | a + b |
22 |
- |
subtraction | a - b |
12 |
* |
multiplication | a * b |
85 |
/ |
division | a / b |
3 (integer division) |
% |
modulus (remainder) | a % b |
2 |
a + b = 22 a - b = 12 a * b = 85 a / b = 3 a % b = 2 17.0 / 5 = 3.4
% works only with integers. For decimals use fmod() from <math.h>. Dividing an integer by zero crashes the program.
Relational operators#
They compare two values and give 1 (true) or 0 (false): ==, !=, >, <, >=, <=.
marks >= 40 : 1 marks == 100: 0 marks != 72 : 0
= is assignment, == is comparison. Writing if (x = 5) assigns 5 to x and is always true - a classic bug. Compile with -Wall and GCC warns you.
Logical operators#
| Operator | Name | True when |
|---|---|---|
&& |
AND | both conditions are true |
|| |
OR | at least one is true |
! |
NOT | the condition is false |
Can vote: 1 Teen or senior: 1 Not adult: 0
C uses short-circuit evaluation: in A && B, if A is false, B is never evaluated. In A || B, if A is true, B is skipped. This lets you write safe checks like if (n != 0 && total / n > 50).
Assignment and compound assignment#
x = 10; // assign
x += 5; // x = x + 5 → 15
x -= 3; // x = x - 3 → 12
x *= 2; // x = x * 2 → 24
x /= 4; // x = x / 4 → 6
x %= 4; // x = x % 4 → 2
Increment and decrement#
++ adds 1 and -- subtracts 1. The position matters when the value is used in the same expression:
a = 6, b = 5 a = 7, c = 7
a++ (post) - पहले पुरानी value इस्तेमाल होती है, फिर a बढ़ता है। ++a (pre) - पहले a बढ़ता है, फिर नई value इस्तेमाल होती है। अकेली line में (a++;) दोनों का असर एक जैसा है। एक ही expression में एक variable को दो बार ++ न करें (a++ + a++) - इसका result undefined होता है।
The ternary (conditional) operator#
condition ? value1 : value2 - a compact if-else that produces a value.
Bigger: 31 Result: PASS
Bitwise operators#
They work on the individual bits of integers - used in embedded systems, flags and interview puzzles.
| Operator | Meaning | 6 op 3 (110, 011) |
|---|---|---|
& |
AND | 2 (010) |
| |
OR | 7 (111) |
^ |
XOR | 5 (101) |
~ |
NOT | ~6 is -7 |
<< |
left shift (× 2ⁿ) | 6 << 1 = 12 |
>> |
right shift (÷ 2ⁿ) | 6 >> 1 = 3 |
x & y = 2 x | y = 7 x ^ y = 5 x << 1 = 12 x >> 1 = 3 Is 6 odd? 0
Precedence - which operator runs first?#
From highest to lowest (most used ones):
()brackets,++--(postfix)!~++--(prefix), unary-, casts,sizeof*/%+-<<>><<=>>===!=&, then^, then|&&, then||?:ternary=+=-=... assignment
14 20 10 3
Operators with the same precedence usually run left to right (20 / 4 * 2 is (20 / 4) * 2 = 10). When unsure, add brackets - they make code clearer for humans too.
Summary#
/between integers drops the fraction;%gives the remainder.- Comparisons and logical operators give
1or0;&&and||short-circuit. a++uses then increments;++aincrements then uses.?:chooses between two values; bitwise operators act on bits.- Use brackets when precedence is not obvious.