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
Remember that % works only with integer operands (5.5 % 2 is a compile error; use fmod() from <math.h> for floating-point values). Since C99, the result takes the sign of the left operand, so -7 % 3 is -1 and 7 % -3 is 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);
Output:
1
In C, a relational expression gives the int value 1 when the condition is true and 0 when it is false. (When C tests a condition, such as in an if, any non-zero value counts as true.)
4. Logical Operators
Logical operators are used to combine or reverse conditions.
Operator | Meaning |
|---|---|
| AND |
| OR |
| NOT |
Example:
int age = 20;
printf("%d", age >= 18 && age <= 60);
Both conditions are true, so the result is true and 1 is printed.
Logical operators also use short-circuit evaluation: in A && B, if A is false, B is not evaluated at all; in A || B, if A is true, B is skipped.
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.
Prefix vs Postfix
These operators can be written before (++x, prefix) or after (x++, postfix) the variable. Both add 1, but the value of the expression is different:
int x = 5;
int y = x++; // y gets 5 (old value), then x becomes 6
int p = 5;
int q = ++p; // p becomes 6 first, then q gets 6
Avoid using ++ or -- on the same variable more than once in a single expression (for example i = i++ + ++i;). Such code has undefined behavior in C, and different compilers can give different results.
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);
Output:
1
In binary, 5 is 101 and 3 is 011, so 101 & 011 = 001, which is 1.
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.
Operator Precedence
When an expression has several operators, precedence decides which one is applied first, just like BODMAS in maths.
int r = 2 + 3 * 4; // 14, because * is done before +
int s = (2 + 3) * 4; // 20, parentheses are done first
A simplified order, from highest to lowest:
Postfix
++--, then unary!~prefix++--and casts*/%+-<<>><<=>>=, then==!=&, then^, then|&&, then||?:=+=-=and other assignmentsComma
,
When in doubt, use parentheses. They make the code easier to read and remove any confusion.
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.