An operator is a symbol used to perform an operation on one or more values or variables. Operators are an important part of expressions in C.
An operator is a symbol used to perform an operation on one or more values or variables. Operators are an important part of expressions in C.
Operators tell the computer what operation to perform, such as add, subtract, compare, assign, or check a condition.
Here + is an operator used to add a and
b, while = is the assignment operator.
| Type | Operators | Purpose |
|---|---|---|
| Arithmetic | + - * / % |
Mathematical calculations |
| Relational | == != > < >= <= |
Compare two values |
| Logical | && || ! |
Combine or reverse conditions |
| Assignment | = += -= *= /= %= |
Assign values to variables |
| Increment / Decrement | ++ -- |
Increase or decrease a value by 1 |
| Bitwise | & | ^ ~ << >> |
Operate on individual bits |
| Conditional | ?: |
Short form of a simple if-else expression |
Arithmetic operators are used to perform mathematical calculations.
| Operator | Operation | Example |
|---|---|---|
+ |
Addition | 10 + 5 = 15 |
- |
Subtraction | 10 - 5 = 5 |
* |
Multiplication | 10 * 5 = 50 |
/ |
Division | 10 / 5 = 2 |
% |
Remainder | 10 % 3 = 1 |
Relational operators compare two values. The result is used as a logical condition.
| Operator | Meaning | Example |
|---|---|---|
== |
Equal to | a == b |
!= |
Not equal to | a != b |
> |
Greater than | a > b |
< |
Less than | a < b |
>= |
Greater than or equal to | a >= b |
<= |
Less than or equal to | a <= b |
Logical operators are used to combine or reverse conditions.
| Operator | Meaning | Example |
|---|---|---|
&& |
Logical AND | a > 5 && b < 10 |
|| |
Logical OR | a > 5 || b < 10 |
! |
Logical NOT | !(a > 5) |
Assignment operators are used to assign or update values stored in variables.
| Operator | Example | Meaning |
|---|---|---|
= |
a = 10 |
Assign 10 to a |
+= |
a += 5 |
a = a + 5 |
-= |
a -= 5 |
a = a - 5 |
*= |
a *= 5 |
a = a * 5 |
/= |
a /= 5 |
a = a / 5 |
The ++ operator increases a value by 1, while the
-- operator decreases a value by 1.
| Form | Meaning |
|---|---|
++a |
Increase first, then use the value. |
a++ |
Use the value first, then increase it. |
--a |
Decrease first, then use the value. |
a-- |
Use the value first, then decrease it. |
Bitwise operators work on the individual bits of integer values. They are mainly useful when low-level or bit-level operations are required.
| Operator | Name |
|---|---|
& |
Bitwise AND |
| |
Bitwise OR |
^ |
Bitwise XOR |
~ |
Bitwise NOT |
<< |
Left Shift |
>> |
Right Shift |
The conditional operator ?: is a
short form for selecting one of two expressions based on a condition.
max = (a > b) ? a : b;
If a > b is true, a is assigned;
otherwise b is assigned.
Operator precedence determines which operator is evaluated first when an expression contains multiple operators.
Consider: 2 + 3 * 4
Multiplication has higher precedence than addition, so: 3 * 4 = 12 is evaluated first.
Final result: 2 + 12 = 14
| Priority | Operators | Example |
|---|---|---|
| 1 — Highest | () |
(a + b) |
| 2 | ++ -- |
++a |
| 3 | * / % |
a * b |
| 4 | + - |
a + b |
| 5 | < <= > >= |
a < b |
| 6 | == != |
a == b |
| 7 | && |
a && b |
| 8 | || |
a || b |
| 9 | ?: |
a ? b : c |
| 10 | Assignment operators | a = b |
When two operators have the same precedence, associativity determines the direction in which they are evaluated.
Multiplication and division have the same precedence and are generally evaluated from left to right.
20 / 5 * 2 → first 20 / 5 = 4 → then 4 * 2 = 8
Parentheses can be used to explicitly control the order of evaluation in an expression.
| Expression | Result |
|---|---|
2 + 3 * 4 |
14 |
(2 + 3) * 4 |
20 |
Parentheses → Increment/Decrement → Multiplication/Division → Addition/Subtraction → Relational → Logical → Conditional → Assignment
| Concept | Remember |
|---|---|
| Arithmetic | Performs mathematical calculations. |
| Relational | Compares values. |
| Logical | Combines conditions. |
| Assignment | Assigns or updates values. |
| ++ / -- | Increase or decrease by 1. |
| Conditional | Short form of simple if-else selection. |
| Precedence | Determines which operator is evaluated first. |
| Associativity | Determines evaluation direction for equal precedence. |
Watch a beginner-friendly explanation of C operators, precedence and associativity.
A short handwritten-style revision sheet for C operators and precedence will be provided here.
Use the mind map for quick revision of C operator categories and precedence.