Chapter 5: Operators in C
1. Arithmetic Operators
These are used for mathematical calculations.
| Operator | Name | Example | Result (if a=10, b=3) |
|---|---|---|---|
| + | Addition | a + b | 13 |
| – | Subtraction | a – b | 7 |
| * | Multiplication | a * b | 30 |
| / | Division | a / b | 3 (integer division) |
| % | Modulus (remainder) | a % b | 1 |
Important Notes:
- / between two integers gives integer division (removes decimal part)
- Use float or double if you want decimal result
Example:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <stdio.h> int main() { int a = 10, b = 3; int sum, diff, product, quotient, remainder; sum = a + b; diff = a - b; product = a * b; quotient = a / b; // 3 (decimal part removed) remainder = a % b; // 1 printf("Sum: %d\n", sum); printf("Difference: %d\n", diff); printf("Product: %d\n", product); printf("Quotient: %d\n", quotient); printf("Remainder: %d\n", remainder); // For decimal division float decimal_div = (float)a / b; // Type casting printf("Decimal division: %.2f\n", decimal_div); return 0; } |
Output:
|
0 1 2 3 4 5 6 7 8 9 10 11 |
Sum: 13 Difference: 7 Product: 30 Quotient: 3 Remainder: 1 Decimal division: 3.33 |
2. Relational Operators
These compare two values and return true (1) or false (0).
| Operator | Meaning | Example | Result (a=10, b=3) |
|---|---|---|---|
| == | Equal to | a == b | 0 (false) |
| != | Not equal to | a != b | 1 (true) |
| > | Greater than | a > b | 1 |
| < | Less than | a < b | 0 |
| >= | Greater than or equal | a >= b | 1 |
| <= | Less than or equal | a <= b | 0 |
Example:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <stdio.h> int main() { int a = 10, b = 3; printf("a == b : %d\n", a == b); // 0 printf("a != b : %d\n", a != b); // 1 printf("a > b : %d\n", a > b); // 1 printf("a < b : %d\n", a < b); // 0 return 0; } |
3. Logical Operators
Used to combine conditions (AND, OR, NOT).
| Operator | Name | Meaning | Example |
|---|---|---|---|
| && | AND | True if both are true | (a > 5 && b < 10) |
|
OR | ||
| ! | NOT | Reverses the value (true → false) | !(a == b) |
Example:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <stdio.h> int main() { int age = 20; int marks = 85; // Check if student is adult AND passed if (age >= 18 && marks >= 40) { printf("You are an adult and passed!\n"); } // Check if student is adult OR has high marks if (age >= 18 || marks >= 90) { printf("You are either adult or topper!\n"); } // NOT operator if (!(age < 18)) { printf("You are NOT a minor.\n"); } return 0; } |
4. Assignment Operators
Used to assign values to variables.
| Operator | Example | Same as |
|---|---|---|
| = | x = 10 | x = 10 |
| += | x += 5 | x = x + 5 |
| -= | x -= 3 | x = x – 3 |
| *= | x *= 2 | x = x * 2 |
| /= | x /= 4 | x = x / 4 |
| %= | x %= 3 | x = x % 3 |
Example:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <stdio.h> int main() { int score = 50; score += 10; // score = 60 score *= 2; // score = 120 score -= 20; // score = 100 printf("Final score: %d\n", score); return 0; } |
5. Increment / Decrement Operators
| Operator | Name | Example | Effect |
|---|---|---|---|
| ++ | Increment | x++ | x = x + 1 (postfix) |
| ++ | Pre-increment | ++x | x = x + 1 (before use) |
| — | Decrement | x– | x = x – 1 (postfix) |
| — | Pre-decrement | –x | x = x – 1 (before use) |
Example:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <stdio.h> int main() { int x = 5; printf("x = %d\n", x); // 5 printf("x++ = %d\n", x++); // 5 (print then increase) printf("Now x = %d\n", x); // 6 printf("++x = %d\n", ++x); // 7 (increase then print) return 0; } |
6. Bitwise Operators
Work on bits (0 and 1) – used in low-level programming.
| Operator | Name | Example (a=5 → 0101, b=3 → 0011) | Result |
|---|---|---|---|
| & | AND | a & b | 1 |
|
OR | `a | |
| ^ | XOR | a ^ b | 6 |
| ~ | NOT (ones complement) | ~a | -6 |
| << | Left shift | a << 1 | 10 |
| >> | Right shift | a >> 1 | 2 |
Example (simple):
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <stdio.h> int main() { int a = 5; // 0101 int b = 3; // 0011 printf("a & b = %d\n", a & b); // 0001 → 1 printf("a | b = %d\n", a | b); // 0111 → 7 printf("a ^ b = %d\n", a ^ b); // 0110 → 6 return 0; } |
7. Precedence and Associativity
Operators have priority order (like BODMAS).
Common Precedence Order (Highest to Lowest):
- ++ — (postfix), ()
- ++ — (prefix), !, ~
- * / %
- + –
- << >>
- < <= > >=
- == !=
- &
- ^
- &&
- ||
- =+=-= etc.
Associativity:
- Most operators are left to right
- Assignment operators are right to left
Example:
|
0 1 2 3 4 5 6 7 |
int result = 10 + 5 * 2; // 5*2 first → 10 + 10 = 20 int x = 5 + 3 * 2 == 11; // 3*2 = 6 → 5+6 = 11 → 11==11 → 1 (true) |
Today’s Homework
- Write a program that takes two numbers from user and performs all arithmetic operations (sum, diff, product, div, mod) and prints them.
- Create a program that checks if a number is even or odd using % operator.
- Write a program using logical operators to check if a person can vote (age >= 18 AND citizen == ‘Y’).
- Experiment with increment/decrement: print values of x++, ++x, x–, –x in different lines.
