Operators in C++
What are Operators in C++?
Operators are special symbols that are used to perform operations on variables and values.
👉 In simple words:
Operators tell the compiler what action to perform.
Example:
Here:
-
+is an operator -
It adds
aandb
Types of Operators in C++
C++ operators are mainly divided into 8 types:
-
Arithmetic Operators
-
Relational Operators
-
Logical Operators
-
Assignment Operators
-
Increment and Decrement Operators
-
Bitwise Operators
-
Conditional (Ternary) Operator
-
Special Operators
1️⃣ Arithmetic Operators
Arithmetic operators are used to perform mathematical operations.
| Operator | Meaning | Example |
|---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus (remainder) | a % b |
Example Program:
2️⃣ Relational Operators
Relational operators are used to compare two values.
They always return true (1) or false (0).
| Operator | Meaning |
|---|---|
== | Equal to |
!= | Not equal |
> | Greater than |
< | Less than |
>= | Greater than or equal |
<= | Less than or equal |
Example:
3️⃣ Logical Operators
Logical operators are used to combine conditions.
| Operator | Meaning |
|---|---|
&& | Logical AND |
| ` | |
! | Logical NOT |
Example:
4️⃣ Assignment Operators
Assignment operators are used to assign values to variables.
| Operator | Example | Meaning |
|---|---|---|
= | a = 10 | Assign |
+= | a += 5 | a = a + 5 |
-= | a -= 5 | a = a - 5 |
*= | a *= 5 | a = a * 5 |
/= | a /= 5 | a = a / 5 |
5️⃣ Increment and Decrement Operators
These operators increase or decrease a value by 1.
| Operator | Meaning |
|---|---|
++ | Increment |
-- | Decrement |
Example:
6️⃣ Bitwise Operators
Bitwise operators work on binary (bits).
| Operator | Meaning |
|---|---|
& | Bitwise AND |
^ | Bitwise XOR |
~ | Bitwise NOT |
<< | Left shift |
>> | Right shift |
Example:
7️⃣ Conditional (Ternary) Operator
This operator is a short form of if-else.
Syntax:
Example:
8️⃣ Special Operators
sizeof Operator
Used to find the size of a variable.
Comma Operator
Used to execute multiple expressions.
Operator Precedence in C++
Operator precedence decides which operator is executed first.
Example:
Output = 20 (because * has higher priority)
✅ Advantages of Operators
-
Makes code shorter
-
Improves readability
-
Saves time
-
Easy to perform complex operations
🔚 Conclusion
Operators are one of the most important concepts in C++.
Without operators, programs cannot perform calculations or logic.

Comments
Post a Comment