➗ Operators in C Language
Types, Examples, and Usage
🔹 What is an Operator?An operator in C is a symbol that performs a specific operation on operands (variables or constants).
Example:
int c = a + b; // + is an operator
📌 Types of Operators in C
C operators are classified into the following types:
1️⃣ Arithmetic Operators
Used to perform mathematical operations.
| Operator | Meaning |
|---|---|
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| / | Division |
| % | Modulus |
#include <stdio.h>
int main() {
int a = 10, b = 3;
printf("Add: %d\n", a + b);
printf("Modulus: %d\n", a % b);
return 0;
}
2️⃣ Relational Operators
Used to compare two values.
| Operator | Meaning |
|---|---|
| == | Equal to |
| != | Not equal |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal |
| <= | Less than or equal |
#include <stdio.h>
int main() {
int x = 5, y = 10;
printf("%d", x < y);
return 0;
}
1 // true
3️⃣ Logical Operators
Used to combine conditions.
| Operator | Meaning |
|---|---|
| && | Logical AND |
| || | Logical OR |
| ! | Logical NOT |
#include <stdio.h>
int main() {
int a = 5, b = 10;
printf("%d", (a < b) && (b > 5));
return 0;
}
4️⃣ Assignment Operators
| Operator | Example |
|---|---|
| = | a = 5 |
| += | a += 5 |
| -= | a -= 3 |
| *= | a *= 2 |
| /= | a /= 2 |
#include <stdio.h>
int main() {
int x = 10;
x += 5;
printf("%d", x);
return 0;
}
5️⃣ Increment and Decrement Operators
Used to increase or decrease a value by 1.
| Operator | Meaning |
|---|---|
| ++ | Increment |
| -- | Decrement |
#include <stdio.h>
int main() {
int a = 5;
printf("%d\n", a++);
printf("%d", ++a);
return 0;
}
6️⃣ Bitwise Operators
Operate at the bit level.
| Operator | Meaning |
|---|---|
| & | Bitwise AND |
| | | Bitwise OR |
| ^ | XOR |
| ~ | Complement |
| << | Left Shift |
| >> | Right Shift |
#include <stdio.h>
int main() {
int a = 5, b = 3;
printf("%d", a & b);
return 0;
}
7️⃣ Conditional (Ternary) Operator
Syntax:
condition ? true_value : false_value;
#include <stdio.h>
int main() {
int a = 10, b = 5;
int max = (a > b) ? a : b;
printf("%d", max);
return 0;
}
8️⃣ Special Operators
| Operator | Purpose |
|---|---|
| sizeof | Returns size of variable |
| , | Comma operator |
| & | Address of variable |
| * | Pointer operator |
#include <stdio.h>
int main() {
int a = 10;
printf("%lu", sizeof(a));
return 0;
}
📚 FAQs on Operators in C
Q1. What is an operator?
An operator is a symbol that performs an operation on operands.
Q2. What is the difference between = and ==?
= assigns value, while == compares values.
Q3. Which operator has the highest precedence?
Unary operators (++ , -- , !) have higher precedence.
Q4. What is the ternary operator?
A conditional operator that works as a short form of if-else.
Q5. What does % operator do?
It returns the remainder after division.
🔑 Key Points to Remember
- Operators perform operations on data
- C has many operator types
- Operator precedence affects results
- Use parentheses for clarity