➕ Lesson 4: Operators in C
⏱️ Estimated time: 20 minutes | Difficulty: Beginner
Math in C
Operators are the symbols we use to perform calculations and logic.
Arithmetic Operators
#include <stdio.h>
int main() {
int a = 10, b = 3;
printf("a + b = %d\n", a + b); // 13
printf("a - b = %d\n", a - b); // 7
printf("a * b = %d\n", a * b); // 30
printf("a / b = %d\n", a / b); // 3
printf("a %% b = %d\n", a % b); // 1 (remainder)
int x = 5;
x++; // 6 (increment)
x--; // 5 (decrement)
return 0;
}
Comparison Operators
Return 1 (true) or 0 (false):
int a = 10, b = 5;
printf("%d\n", a == b); // 0 (equal to)
printf("%d\n", a != b); // 1 (not equal)
printf("%d\n", a > b); // 1 (greater than)
printf("%d\n", a < b); // 0 (less than)
printf("%d\n", a >= 10); // 1 (greater or equal)
printf("%d\n", b <= 5); // 1 (less or equal)
Logical Operators
int x = 1, y = 0;
printf("%d\n", x && y); // 0 (AND: both must be true)
printf("%d\n", x || y); // 1 (OR: at least one true)
printf("%d\n", !x); // 0 (NOT: reverses)
int age = 25, hasLicense = 1;
if (age >= 18 && hasLicense) {
printf("You can drive!\n");
}
Assignment Operators
int c = 10;
c += 5; // c = 15
c -= 3; // c = 12
c *= 2; // c = 24
c /= 4; // c = 6
c %= 4; // c = 2
✅ Quick Quiz
❓ What is 10 % 3?
❓ What does && mean?
❓ What does x += 5 mean?