Operators in C Programming
Here is the complete explanation of Operators in C with syntax and examples:
⭐ Operators in C Programming
Operators are symbols used to perform operations on variables and values.
Example:
int a = 10 + 5;
Here + is an operator.
✔ Types of Operators in C
| Category | Operators Included |
|---|---|
| 1️⃣ Arithmetic Operators | +, -, *, /, % |
| 2️⃣ Relational (Comparison) Operators | >, <, >=, <=, ==, != |
| 3️⃣ Logical Operators | &&, ||, ! |
| 4️⃣ Assignment Operators | =, +=, -=, *=, /=, %= |
| 5️⃣ Increment/Decrement Operators | ++, -- |
| 6️⃣ Bitwise Operators | &, |, ^, ~, <<, >> |
| 7️⃣ Conditional (Ternary) Operator | ?: |
| 8️⃣ Special Operators | sizeof, &, *, , , -> |
π 1️⃣ Arithmetic Operators
Used to perform mathematical operations.
| Operator | Meaning |
|---|---|
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| / | Division |
| % | Modulus (Remainder) |
Example:
int a = 10, b = 3;
printf("%d", a % b); // Output: 1
π 2️⃣ Relational Operators
Used to compare two values. Output is True(1) / False(0).
| Operator | Meaning |
|---|---|
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
| == | Equal to |
| != | Not equal to |
Example:
(a > b)
π 3️⃣ Logical Operators
Used to combine relational expressions.
| Operator | Meaning |
|---|---|
| && | Logical AND |
| || | Logical OR |
| ! | Logical NOT |
Example:
if(a > b && a > c)
π 4️⃣ Assignment Operators
| Operator | Example | Same As |
|---|---|---|
| = | a = b | a = b |
| += | a += 5 | a = a + 5 |
| -= | a -= 3 | a = a - 3 |
| *= | a *= 2 | a = a * 2 |
| /= | a /= 4 | a = a / 4 |
| %= | a %= 2 | a = a % 2 |
π 5️⃣ Increment & Decrement Operators
| Operator | Meaning |
|---|---|
| ++ | Increment (Increase by 1) |
| -- | Decrement (Decrease by 1) |
Two Types:
-
Pre:
++a(increment then use) -
Post:
a++(use then increment)
Example:
int a = 5;
printf("%d", a++); // Output: 5 (then becomes 6)
printf("%d", ++a); // Output: 7
π 6️⃣ Bitwise Operators
Works on bits (0 or 1).
| Operator | Meaning |
|---|---|
| & | Bitwise AND |
| | | Bitwise OR |
| ^ | XOR |
| ~ | NOT |
| << | Left Shift |
| >> | Right Shift |
Example:
int a = 5 << 1; // 5 = 0101 → 1010 = 10
π 7️⃣ Conditional / Ternary Operator
Short form of if-else
Syntax:
(condition) ? expression1 : expression2;
Example:
int a = 10, b = 5;
int max = (a > b) ? a : b;
π 8️⃣ Special Operators
| Operator | Meaning |
|---|---|
| sizeof | Returns size of data type/variable |
| & | Address of variable |
| * | Pointer dereference |
| , | Comma (evaluate multiple expressions) |
| -> | Used with pointer to access structure member |
Example:
int a;
printf("%lu", sizeof(a)); // Size of int
π― Operator Precedence (Highest to Lowest)
-
(),[],->,. -
!,~,++,--,(type)cast -
*,/,% -
+,- -
<<,>> -
<,<=,>,>= -
==,!= -
& -
^ -
| -
&& -
|| -
?: -
=,+=,-=, etc. -
,
✔ Simple Example Program
#include <stdio.h>
int main() {
int a = 10, b = 3;
printf("a+b = %d\n", a+b);
printf("a>b = %d\n", a>b);
printf("a&&b = %d\n", a&&b);
return 0;
}
If you want, I can also give you:
✔ Practice Questions
✔ PDF Notes
✔ MCQs with Answers
✔ Viva Questions
Would you like that?
Comments
Post a Comment