Operators in C
Operators are symbols used to perform operations on variables and values. They tell the computer what action to perform.
Without operators, a program cannot calculate, compare, or make decisions. Understanding operators clearly is essential before moving further in C.
Why Operators Matter
Every meaningful program performs operations such as:
- Adding numbers
- Comparing values
- Making decisions
- Updating variables
All of this is done using operators.
Types of Operators in C
C provides several types of operators. In this lesson, we will focus on the most important ones.
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Assignment Operators
1. Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
+Addition-Subtraction*Multiplication/Division%Modulus (remainder)
#include <stdio.h>
int main() {
int a = 10, b = 3;
printf("Addition: %d\n", a + b);
printf("Subtraction: %d\n", a - b);
printf("Multiplication: %d\n", a * b);
printf("Division: %d\n", a / b);
printf("Remainder: %d\n", a % b);
return 0;
}
The modulus operator (%) is often used to check
whether a number is even or odd.
2. Relational Operators
Relational operators are used to compare two values.
They always return either true (1) or false (0).
==Equal to!=Not equal to>Greater than<Less than>=Greater than or equal to<=Less than or equal to
#include <stdio.h>
int main() {
int x = 5, y = 10;
printf("%d\n", x > y);
printf("%d\n", x < y);
printf("%d\n", x == y);
return 0;
}
3. Logical Operators
Logical operators are used when working with multiple conditions.
&&Logical AND||Logical OR!Logical NOT
These operators are heavily used in decision making.
#include <stdio.h>
int main() {
int age = 20;
if (age >= 18 && age <= 60) {
printf("Eligible\n");
}
return 0;
}
4. Assignment Operators
Assignment operators are used to assign values to variables.
=Simple assignment+=Add and assign-=Subtract and assign*=Multiply and assign/=Divide and assign
int a = 10;
a += 5; // a = a + 5
Mini Practice
- Write a program to check if a number is even or odd
- Compare two numbers and print the larger one
- Use logical operators to validate age eligibility
Quick Quiz
Q1. Which operator gives the remainder?
The modulus (%) operator.
Q2. What does the == operator do?
It checks whether two values are equal.
Q3. Which operator is used for logical AND?
The && operator.
Q4. What is the output of 10 % 3?
1
Q5. Does relational operator return true or false?
Yes, it returns true (1) or false (0).