Jan 22, 2026
## Chapter 3: Operators
**What are Operators?**
Operators are symbols that help the computer perform:
* Calculations
* Or comparisons
### 1. Arithmetic Operators
```c
#include
int main() {
int a = 10, b = 3;
printf("Addition: %d + %d = %dn", a, b, a + b); // 13
printf("Subtraction: %d - %d = %dn", a, b, a - b); // 7
printf("Multiplication: %d * %d = %dn", a, b, a * b); // 30
printf("Division: %d / %d = %dn", a, b, a / b); // 3 (integer division)
printf("Modulus: %d %% %d = %dn", a, b, a % b); // 1 (remainder)
return 0;
}
```
Important note on division:
When dividing two integer values:
```c
10 / 3 = 3
```
But if you want a decimal result, then:
```c
10.0 / 3
(float)10 / 3
```
The result will be:
```
3.333333
```
### 2. Relational Operators (for making comparisons)
Let's say:
```c
int x = 5, y = 10;
```
* x == y: Checks if both are equal. 5 and 10 are not equal -> False (0)
* x != y: Checks if both are different. 5 and 10 are different -> True (1)
* x < y: Is X less than Y? 5 is less than 10 -> True
* x > y: Is X greater than Y? 5 is not greater than 10 -> False
* x <= y: Less than or equal to. 5 <= 10 -> True
* x >= y: Greater than or equal to. 5 >= 10 is not true -> False
These operators are used for comparisons.
### 3. Logical Operators
AND operator (&&)
* Both conditions must be true
```c
age >= 18 && age <= 65
```
Meaning:
Age should be greater than or equal to 18 and less than or equal to 65
OR operator (||)
* At least one condition must be true
```c
day == 0 || day == 6
```
Meaning:
If day is 0 or 6 -> holiday (weekend)
NOT operator (!)
* Reverses the condition
```c
! (temperature > 30)
```
Meaning:
Temperature is not greater than 30, meaning it's not very hot
### 4. Assignment Operators
These operators are used to update the value of a variable.
For example:
```c
int score = 50;
```
* score += 10; Meaning: Add 10 to score. Now score = 60
* score -= 5; Subtract 5 from score. Now score = 55
* score *= 2; Multiply score by 2. Now score = 110
* score /= 5; Divide score by 5. Now score = 22
* score %= 3; The remainder after dividing the score by 3. Now score = 1
This is a short form, so that the code remains short and easy.
### 5. Increment and Decrement
This is used to increase or decrease the value by 1.
For example:
```c
int count = 5;
```
* count++; First the value is used, then it increases by 1. This is called **post-increment**.
* ++count; First it increases by 1, then the value is used. This is called **pre-increment**.
* count--; First the value is used, then it decreases by 1. Post-decrement
* --count; First it decreases by 1, then the value is used. Pre-decrement
**Understand with an example:**
```c
int a = 5;
int b = a++;
```
b gets 5. After that, a becomes 6.
```c
int x = 5;
int y = ++x;
```
First x becomes 6. Then y gets 6.
---