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:

int a = 10; int b = 5; int sum = a + b;

Here:

  • + is an operator

  • It adds a and b


Types of Operators in C++

C++ operators are mainly divided into 8 types:

  1. Arithmetic Operators

  2. Relational Operators

  3. Logical Operators

  4. Assignment Operators

  5. Increment and Decrement Operators

  6. Bitwise Operators

  7. Conditional (Ternary) Operator

  8. Special Operators


1️⃣ Arithmetic Operators

Arithmetic operators are used to perform mathematical operations.

OperatorMeaningExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Modulus (remainder)a % b

Example Program:

#include <iostream> using namespace std; int main() { int a = 10, b = 3; cout << "Addition: " << a + b << endl; cout << "Subtraction: " << a - b << endl; cout << "Multiplication: " << a * b << endl; cout << "Division: " << a / b << endl; cout << "Remainder: " << a % b << endl; return 0; }

2️⃣ Relational Operators

Relational operators are used to compare two values.
They always return true (1) or false (0).

OperatorMeaning
==Equal to
!=Not equal
>Greater than
<Less than
>=Greater than or equal
<=Less than or equal

Example:

int a = 10, b = 5; cout << (a > b); // Output: 1 (true)

3️⃣ Logical Operators

Logical operators are used to combine conditions.

OperatorMeaning
&&Logical AND
`
!Logical NOT

Example:

int a = 10, b = 5; if (a > 5 && b < 10) { cout << "Condition is true"; }

4️⃣ Assignment Operators

Assignment operators are used to assign values to variables.

OperatorExampleMeaning
=a = 10Assign
+=a += 5a = a + 5
-=a -= 5a = a - 5
*=a *= 5a = a * 5
/=a /= 5a = a / 5

5️⃣ Increment and Decrement Operators

These operators increase or decrease a value by 1.

OperatorMeaning
++Increment
--Decrement

Example:

int a = 5; cout << ++a; // Output: 6 (pre-increment) cout << a++; // Output: 6 (post-increment)

6️⃣ Bitwise Operators

Bitwise operators work on binary (bits).

OperatorMeaning
&Bitwise AND


^Bitwise XOR
~Bitwise NOT
<<Left shift
>>Right shift

Example:

int a = 5, b = 3; cout << (a & b);

7️⃣ Conditional (Ternary) Operator

This operator is a short form of if-else.

Syntax:

condition ? expression1 : expression2;

Example:

int a = 10, b = 5; int max = (a > b) ? a : b; cout << max;

8️⃣ Special Operators

sizeof Operator

Used to find the size of a variable.

cout << sizeof(int);

Comma Operator

Used to execute multiple expressions.

int a = (5, 10);

Operator Precedence in C++

Operator precedence decides which operator is executed first.

Example:

int result = 10 + 5 * 2;

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

Popular posts from this blog

C++ Programming for Beginners – Step by Step Guide

If-Else Statement in C++

Variables and Data Types in C++