Operators in C
Operators in C
Introduction
In C programming, operators are used to perform operations on variables and values.
They are essential for performing calculations, comparisons, and logical decisions in programs.
👉 In simple terms:
Operators are symbols that perform operations on operands (values).
What are Operators?
An operator is a symbol that tells the compiler to perform a specific operation.
👉 Example:
int a = 10 + 5;
Here:
- + → operator
-
10and5→ operands
Types of Operators in C
C provides several types of operators:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Assignment Operators
- Increment & Decrement Operators
- Bitwise Operators
- Conditional (Ternary) Operator
- Special Operators
1. Arithmetic Operators
Used for mathematical calculations:
| Operator | Description | Example |
|---|---|---|
| + | Addition | a + b |
| - | Subtraction | a - b |
| * | Multiplication | a * b |
| / | Division | a / b |
| % | Modulus (remainder) | a % b |
Example
#include <stdio.h> int main() { int a = 10, b = 3; printf("Addition = %d\n", a + b); printf("Subtraction = %d\n", a - b); printf("Multiplication = %d\n", a * b); printf("Division = %d\n", a / b); printf("Modulus = %d\n", a % b); return 0; }
Output
Addition = 13 Subtraction = 7 Multiplication = 30 Division = 3 Modulus = 1
2. Relational Operators
Used to compare values:
| Operator | Meaning | Example |
|---|---|---|
| == | Equal to | a == b |
| != | Not equal | a != b |
| > | Greater than | a > b |
| < | Less than | a < b |
| >= | Greater or equal | a >= b |
| <= | Less or equal | a <= b |
Example
Output
3. Logical Operators
Used for decision making:
| Operator | Meaning | Example |
|---|---|---|
| && | Logical AND | a > 0 && b > 0 |
| || | Logical OR | a > 0 || b > 0 |
| ! | Logical NOT | !(a > b) |
Example
Output
4. Assignment Operators
Used to assign values:
| Operator | Description | Example |
|---|---|---|
| = | Assign | a = 10 |
| += | Add and assign | a += 5 |
| -= | Subtract and assign | a -= 5 |
| *= | Multiply and assign | a *= 2 |
| /= | Divide and assign | a /= 2 |
Example
Output
5. Increment & Decrement Operators
| Operator | Description | Example |
|---|---|---|
| ++ | Increment (increase by 1) | a++ or ++a |
| -- | Decrement (decrease by 1) | a-- or --a |
Example 1
int x = 5;
x++; // x becomes 6
x--; // x becomes 5
Example 2
Output
6. Bitwise Operators
Used for bit-level operations:
| Operator | Description | Example |
|---|---|---|
| & | Bitwise AND | a & b |
| | | Bitwise OR | a | b |
| ^ | Bitwise XOR | a ^ b |
| ~ | Bitwise NOT | ~a |
| << | Left Shift | a << 1 |
| >> | Right Shift | a >> 1 |
Example
Output
7. Conditional (Ternary) Operator
Used as a shortcut for if-else:
condition ? expression1 : expression2;
Example
#include <stdio.h> int main() { int a = 10, b = 20; int max = (a > b) ? a : b; printf("Max = %d", max); return 0; }
Output
8. Special Operators
| Operator | Description | Example |
|---|---|---|
| sizeof | Returns size of variable | sizeof(int) |
| & | Address of operator | &a |
| * | Pointer operator | *ptr |
Example
Output
Operator Precedence
Operators follow priority rules:
Example:
int result = 10 + 5 * 2;
Output:
20
✔ Multiplication happens before addition
Common Mistakes
-
❌ Confusing
=and== - ❌ Division with integers
- ❌ Incorrect use of logical operators
- ❌ Forgetting precedence rules
Pro Tips
- ✔ Use parentheses to control operations
- ✔ Practice each operator type
- ✔ Understand precedence clearly
- ✔ Write clean expressions
Conclusion
Operators are essential in C programming. They allow you to perform calculations, comparisons, and logical decisions effectively.
Mastering operators is a key step toward becoming a strong programmer.
Great work. It will helpful for me. Thankyou
ReplyDeleteThank you, YOKESH! 😊
DeleteHappy to hear that it helped you. Your support means a lot! 🙌