Operators in C
Operators are special symbols used to perform operations on variables and values.
C provides a rich set of operators for arithmetic, comparison, logical evaluation, bit manipulation, and more.
Types of Operators in C
1. Arithmetic Operators 2. Relational Operators 3. Logical Operators 4. Assignment Operators 5. Increment/Decrement Operators 6. Bitwise Operators 7. Conditional (Ternary) Operator 8. Special Operators (sizeof, comma, etc.)
1. Arithmetic Operators
+ addition - subtraction * multiplication / division % modulus (remainder)
Example
#include <stdio.h>
int main() {
int a = 10, b = 3;
printf("Sum = %d\n", a + b);
printf("Difference = %d\n", a - b);
printf("Product = %d\n", a * b);
printf("Division = %d\n", a / b);
printf("Remainder = %d\n", a % b);
return 0;
}
2. Relational Operators
== equal to != not equal to > greater than < less than >= greater or equal <= less or equal
Example
#include <stdio.h>
int main() {
int x = 5, y = 8;
printf("%d\n", x == y);
printf("%d\n", x != y);
printf("%d\n", x > y);
printf("%d\n", x < y);
return 0;
}
3. Logical Operators
&& logical AND || logical OR ! logical NOT
Example
#include <stdio.h>
int main() {
int a = 5, b = 10;
printf("%d\n", (a > 2 && b < 20));
printf("%d\n", (a == 5 || b == 2));
printf("%d\n", !(a == 5));
return 0;
}
4. Assignment Operators
= simple assignment += add and assign -= subtract and assign *= multiply and assign /= divide and assign %= modulus and assign
Example
int x = 10; x += 5; // 15 x -= 3; // 12
5. Increment & Decrement Operators
++a pre-increment a++ post-increment --a pre-decrement a-- post-decrement
Example
#include <stdio.h>
int main() {
int a = 10;
printf("%d\n", ++a); // 11
printf("%d\n", a++); // 11 (prints old value)
printf("%d\n", a); // 12
return 0;
}
6. Bitwise Operators
& AND | OR ^ XOR ~ NOT << left shift >> right shift
Example
#include <stdio.h>
int main() {
int a = 5, b = 3;
printf("a & b = %d\n", a & b);
printf("a | b = %d\n", a | b);
printf("a ^ b = %d\n", a ^ b);
printf("~a = %d\n", ~a);
printf("a << 1 = %d\n", a << 1);
printf("a >> 1 = %d\n", a >> 1);
return 0;
}
7. Conditional (Ternary) Operator
The ternary operator is a shorthand for if-else.
(condition) ? value_if_true : value_if_false
Example
#include <stdio.h>
int main() {
int age = 18;
char *msg = (age >= 18) ? "Adult" : "Minor";
printf("%s", msg);
return 0;
}
8. Special Operators
sizeof Operator
printf("%lu", sizeof(int));
printf("%lu", sizeof(3.14));
Comma Operator
int a = (5, 10); // a = 10
Practical Example – Combine Multiple Operators
#include <stdio.h>
int main() {
int a = 5, b = 10, c;
c = (a < b && b != 0) ? (a + b) : 0;
printf("Result = %d", c);
return 0;
}
Practice Questions
- What is an operator? List different types.
- Explain logical operators with examples.
- Write a program to demonstrate arithmetic and relational operators.
- What is the difference between pre-increment and post-increment?
- Write a program to show bitwise operators.
Practice Task
Write a program that takes two numbers from the user and displays:
• Their sum
• Their comparison (greater/less/equal)
• Bitwise AND, OR, XOR
• Which number is larger using a ternary operator