In programming, we often need to perform operations such as addition, subtraction, comparison, or assignment.
C provides special symbols called operators to perform these operations.
What is an Operator?
An operator is a symbol that tells the compiler to perform a specific operation on one or more values.
For example:
int sum = 10 + 20;
Here, + is an operator that adds two numbers.
The values on which an operator works are called operands.
10 + 20
↑ ↑
Operand
+ → Operator
Types of Operators in C
C operators can be grouped into several categories:
Type | Examples |
|---|---|
Arithmetic |
|
Assignment |
|
Relational |
|
Logical |
|
Increment/Decrement |
|
Bitwise |
|
Conditional |
|
Other |
|
Let's understand the most important ones.
1. Arithmetic Operators
These are used for mathematical calculations.
Operator | Meaning |
|---|---|
| Addition |
| Subtraction |
| Multiplication |
| Division |
| Remainder |
Example:
int a = 10;
int b = 3;
printf("%d", a + b);
Output:
13
The % operator gives the remainder:
printf("%d", 10 % 3);
Output:
1
2. Assignment Operators
Assignment operators are used to assign or update values.
The basic assignment operator is =.
int age = 20;
Other assignment operators include:
age += 5; // age = age + 5
age -= 5; // age = age - 5
age *= 2; // age = age * 2
age /= 2; // age = age / 2
3. Relational Operators
Relational operators are used to compare values.
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
== Equal to
!= Not equal to
For example:
int age = 20;
printf("%d", age > 18);
The result is non-zero when the condition is true and 0 when it is false. In typical C programs, 1 is commonly used to represent true.
4. Logical Operators
Logical operators are used to combine or reverse conditions.
Operator | Meaning |
|---|---|
| AND |
` | |
| NOT |
Example:
int age = 20;
printf("%d", age >= 18 && age <= 60);
Both conditions are true, so the result is true.
5. Increment and Decrement
These operators increase or decrease a value by 1.
int count = 5;
count++;
Now count becomes 6.
Similarly:
count--;
decreases it by 1.
You'll commonly see these operators in loops.
6. Bitwise Operators
Bitwise operators work directly with the individual bits of integer values.
Examples include:
& AND
| OR
^ XOR
~ NOT
<< Left shift
>> Right shift
For example:
int a = 5;
int b = 3;
printf("%d", a & b);
Bitwise operators are especially useful in areas such as embedded systems, low-level programming, and bit manipulation.
7. Conditional Operator
The conditional operator ?: is a short way to choose between two expressions based on a condition.
int age = 20;
int result = (age >= 18) ? 1 : 0;
If age >= 18 is true, result gets 1; otherwise, it gets 0.
Simple Example
Let's combine a few operators:
#include <stdio.h>
int main()
{
int a = 10;
int b = 5;
printf("Sum = %d\n", a + b);
printf("Difference = %d\n", a - b);
printf("Product = %d\n", a * b);
printf("Remainder = %d\n", a % b);
return 0;
}
Output:
Sum = 15
Difference = 5
Product = 50
Remainder = 0
In Simple Words
Operators are symbols used to perform operations on values and variables.
For example:
10 + 5
Here, + is the operator, while 10 and 5 are the operands.
Operators are one of the most important parts of C because almost every program uses them for calculations, comparisons, conditions, and data manipulation.