Welcome to The Coding College! In this tutorial, we’ll explore operators in C programming, essential tools for performing operations on variables and values. Operators are the building blocks of logic and computation in any C program, making them a must-know for every developer.
What Are Operators in C?
An operator in C is a symbol that tells the compiler to perform specific operations. These operations can range from arithmetic to logical, relational, or bitwise manipulations.
For example:
int a = 10, b = 5;
int sum = a + b; // '+' is an operator
Types of Operators in C
C supports various operators, broadly classified as:
- Arithmetic Operators
- Relational (Comparison) Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Unary Operators
- Ternary Operators
- Special Operators
1. Arithmetic Operators
These operators perform basic mathematical operations.
Operator | Description | Example | Output |
---|---|---|---|
+ | Addition | a + b | Sum of a and b |
- | Subtraction | a - b | Difference of a and b |
* | Multiplication | a * b | Product of a and b |
/ | Division | a / b | Quotient of a and b |
% | Modulus (remainder) | a % b | Remainder when a is divided by b |
Example:
#include <stdio.h>
int main() {
int a = 10, b = 3;
printf("Addition: %d\n", a + b);
printf("Modulus: %d\n", a % b);
return 0;
}
Output:
Addition: 13
Modulus: 1
2. Relational (Comparison) Operators
Used to compare two values and return true (1)
or false (0)
.
Operator | Description | Example | Output |
---|---|---|---|
== | Equal to | a == b | 1 (true) or 0 (false) |
!= | Not equal to | a != b | 1 (true) or 0 (false) |
> | Greater than | a > b | 1 or 0 |
< | Less than | a < b | 1 or 0 |
>= | Greater than or equal to | a >= b | 1 or 0 |
<= | Less than or equal to | a <= b | 1 or 0 |
3. Logical Operators
These operators combine two or more conditions.
Operator | Description | Example | Output |
---|---|---|---|
&& | Logical AND | (a > b) && (a > 0) | 1 if both are true |
` | ` | Logical OR | |
! | Logical NOT | !(a > b) | Reverses the condition |
4. Bitwise Operators
Perform bit-level operations.
Operator | Description | Example |
---|---|---|
& | Bitwise AND | a & b |
` | ` | Bitwise OR |
^ | Bitwise XOR | a ^ b |
~ | Bitwise NOT | ~a |
<< | Left Shift | a << 2 |
>> | Right Shift | a >> 2 |
5. Assignment Operators
Used to assign values to variables.
Operator | Description | Example |
---|---|---|
= | Assign | a = 10 |
+= | Add and assign | a += 5 |
-= | Subtract and assign | a -= 5 |
*= | Multiply and assign | a *= 5 |
/= | Divide and assign | a /= 5 |
%= | Modulus and assign | a %= 5 |
6. Unary Operators
Operate on a single operand.
Operator | Description | Example |
---|---|---|
+ | Unary plus | +a |
- | Unary minus | -a |
++ | Increment | ++a or a++ |
-- | Decrement | --a or a-- |
7. Ternary Operator
The only ternary operator in C:
(condition) ? expression1 : expression2
Example:
#include <stdio.h>
int main() {
int a = 10, b = 5;
int max = (a > b) ? a : b;
printf("Maximum: %d\n", max);
return 0;
}
Output:
Maximum: 10
8. Special Operators
- Comma Operator (
,
): Used to separate multiple expressions. - Pointer Operators (
*
,&
): Used in pointer operations. - Sizeof Operator: Returns the size of a variable or data type.
Examples of Operator Usage in C
Arithmetic Operators Example:
#include <stdio.h>
int main() {
int a = 15, b = 4;
printf("Sum: %d\n", a + b);
printf("Difference: %d\n", a - b);
printf("Product: %d\n", a * b);
printf("Quotient: %d\n", a / b);
printf("Remainder: %d\n", a % b);
return 0;
}
Output:
Sum: 19
Difference: 11
Product: 60
Quotient: 3
Remainder: 3
Conclusion
Operators in C are essential for performing operations, comparisons, and logical decisions in your programs. Whether it’s basic arithmetic or complex bit-level manipulation, mastering operators is key to becoming a proficient C programmer.