Operators in C
An operator is a symbol that tells the compiler to perform a specific operation on one or more operands. Operators are the building blocks of expressions and computations in C. For example, in 5 + 3, the symbol + is the operator and 5 and 3 are the operands.
C has a rich collection of operators organized into different categories based on their purpose.
Types of Operators in C
- Arithmetic Operators
- Relational (Comparison) Operators
- Logical Operators
- Assignment Operators
- Increment and Decrement Operators
- Bitwise Operators
- Conditional (Ternary) Operator
- sizeof Operator
- Comma Operator
1. Arithmetic Operators
Used to perform basic mathematical operations like addition, subtraction, multiplication, and division.
| Operator | Name | Example | Result |
|---|---|---|---|
| + | Addition | 10 + 3 | 13 |
| - | Subtraction | 10 - 3 | 7 |
| * | Multiplication | 10 * 3 | 30 |
| / | Division | 10 / 3 | 3 (integer division) |
| % | Modulus (Remainder) | 10 % 3 | 1 |
#include <stdio.h>
int main()
{
int a = 10, b = 3;
printf("Addition : %d\n", a + b); // 13
printf("Subtraction : %d\n", a - b); // 7
printf("Multiplication : %d\n", a * b); // 30
printf("Division : %d\n", a / b); // 3
printf("Modulus : %d\n", a % b); // 1
return 0;
}
Note: When both operands are integers, division gives an integer result. Use float to get decimal results.
float result = (float)10 / 3;
printf("%.2f", result); // Output: 3.33
2. Relational (Comparison) Operators
Used to compare two values. The result is either 1 (true) or 0 (false).
| Operator | Meaning | Example | Result |
|---|---|---|---|
| == | Equal to | 5 == 5 | 1 (true) |
| != | Not equal to | 5 != 3 | 1 (true) |
| > | Greater than | 7 > 4 | 1 (true) |
| < | Less than | 3 < 8 | 1 (true) |
| >= | Greater than or equal to | 5 >= 5 | 1 (true) |
| <= | Less than or equal to | 3 <= 2 | 0 (false) |
#include <stdio.h>
int main()
{
int x = 8, y = 5;
printf("%d == %d : %d\n", x, y, x == y); // 0
printf("%d != %d : %d\n", x, y, x != y); // 1
printf("%d > %d : %d\n", x, y, x > y); // 1
printf("%d < %d : %d\n", x, y, x < y); // 0
return 0;
}
3. Logical Operators
Used to combine multiple conditions. Commonly used in if statements and loops.
| Operator | Name | Description | Example |
|---|---|---|---|
| && | Logical AND | True only if both conditions are true | (5 > 3) && (8 > 6) → 1 |
| || | Logical OR | True if at least one condition is true | (5 > 3) || (2 > 8) → 1 |
| ! | Logical NOT | Reverses the result | !(5 > 3) → 0 |
#include <stdio.h>
int main()
{
int age = 20;
int hasID = 1;
// AND: both must be true
if (age >= 18 && hasID == 1)
printf("Entry allowed\n");
// OR: at least one must be true
if (age >= 18 || hasID == 1)
printf("May enter\n");
// NOT: reverses the result
if (!hasID)
printf("No ID found\n");
else
printf("ID is present\n");
return 0;
}
Output
Entry allowed
May enter
ID is present
4. Assignment Operators
Used to assign values to variables. The = operator assigns the value on the right to the variable on the left. Compound assignment operators combine an arithmetic operation with assignment.
| Operator | Example | Equivalent To |
|---|---|---|
| = | a = 5 | a = 5 |
| += | a += 3 | a = a + 3 |
| -= | a -= 3 | a = a - 3 |
| *= | a *= 3 | a = a * 3 |
| /= | a /= 3 | a = a / 3 |
| %= | a %= 3 | a = a % 3 |
#include <stdio.h>
int main()
{
int a = 10;
a += 5; printf("After += 5: %d\n", a); // 15
a -= 3; printf("After -= 3: %d\n", a); // 12
a *= 2; printf("After *= 2: %d\n", a); // 24
a /= 4; printf("After /= 4: %d\n", a); // 6
a %= 4; printf("After %%= 4: %d\n", a); // 2
return 0;
}
5. Increment and Decrement Operators
Used to increase or decrease the value of a variable by 1.
| Operator | Name | Description |
|---|---|---|
| ++a | Pre-increment | Increment first, then use the value |
| a++ | Post-increment | Use the value first, then increment |
| --a | Pre-decrement | Decrement first, then use the value |
| a-- | Post-decrement | Use the value first, then decrement |
#include <stdio.h>
int main()
{
int a = 5;
printf("a = %d\n", a); // 5
printf("a++ = %d\n", a++); // 5 (uses 5, then a becomes 6)
printf("a = %d\n", a); // 6
printf("++a = %d\n", ++a); // 7 (increments first, then uses 7)
printf("a = %d\n", a); // 7
return 0;
}
Output
a = 5
a++ = 5
a = 6
++a = 7
a = 7
6. Conditional (Ternary) Operator
The ternary operator is a shorthand for a simple if-else statement. It takes three operands.
Syntax
condition ? value_if_true : value_if_false;
#include <stdio.h>
int main()
{
int marks = 75;
char *result;
result = (marks >= 50) ? "Pass" : "Fail";
printf("Result: %s\n", result);
return 0;
}
Output
Result: Pass
7. Bitwise Operators
Operate directly on the binary (bit-level) representation of a number. Used in low-level programming, embedded systems, and performance-critical code.
| Operator | Name | Example |
|---|---|---|
| & | Bitwise AND | 5 & 3 = 1 |
| | | Bitwise OR | 5 | 3 = 7 |
| ^ | Bitwise XOR | 5 ^ 3 = 6 |
| ~ | Bitwise NOT | ~5 = -6 |
| << | Left Shift | 5 << 1 = 10 |
| >> | Right Shift | 5 >> 1 = 2 |
8. sizeof Operator
Returns the size in bytes of a variable or data type.
#include <stdio.h>
int main()
{
int num = 10;
printf("Size of int : %zu bytes\n", sizeof(int));
printf("Size of num : %zu bytes\n", sizeof(num));
printf("Size of double : %zu bytes\n", sizeof(double));
return 0;
}
Operator Precedence in C
When multiple operators appear in an expression, C evaluates them based on precedence (priority). Higher precedence operators are evaluated first. Operators at the same level are evaluated left-to-right (left associativity) or right-to-left (right associativity).
| Precedence | Operators | Associativity |
|---|---|---|
| 1 (Highest) | () [] . -> | Left to Right |
| 2 | ++ -- ! ~ (unary) | Right to Left |
| 3 | * / % | Left to Right |
| 4 | + - | Left to Right |
| 5 | << >> | Left to Right |
| 6 | < <= > >= | Left to Right |
| 7 | == != | Left to Right |
| 8 | & | ^ | Left to Right |
| 9 | && || | Left to Right |
| 10 | ?: | Right to Left |
| 11 (Lowest) | = += -= etc. | Right to Left |
Precedence Example
int result = 2 + 3 * 4;
// * has higher precedence than +
// So: 2 + (3 * 4) = 2 + 12 = 14
printf("%d", result); // Output: 14
int result2 = (2 + 3) * 4;
// Parentheses force evaluation first
// So: (5) * 4 = 20
printf("%d", result2); // Output: 20
Summary
C provides a comprehensive set of operators for performing arithmetic, comparisons, logical decisions, assignments, and low-level bit manipulation. Understanding how each operator works and how operator precedence affects expression evaluation is crucial for writing correct and efficient C programs. Using parentheses whenever there is any doubt about the order of operations is always a good practice.
