What are Operators in C++ ,Types, Practical Examples

Operators are symbols that perform operations on values (called operands). Think of them like math symbols (+, -, ×, ÷) or comparison words (greater than, equal to).

In programming, operators help:

  • Manipulate data (numbers, text, etc.).
  • Control how a program works (decision-making, loops).

C++ provides many operators with the ability to perform different tasks. Understanding them helps you write better and faster code.

Types of Operators in C++

The following are different Operators in C++.

1. Arithmetic Operators

Arithmetic Operators perform mathematical calculations. Here are types of arithmetic operators:

a. Binary Arithmetic Operators (Two Operands)

These work with two numbers:

OperatorMeaningExample
+Addition5 + 3 → 8
-Subtraction5 - 3 → 2
*Multiplication5 * 3 → 15
/Division10 / 3 → 3 (integer) or 3.333 (float)
%Modulo (remainder)10 % 3 → 1

Note:

  • Integer division (int / int) Discards the decimal part.
  • Use float or double For exact division.

b. Unary Arithmetic Operators (One Operand)

These work with a single number:

OperatorMeaningExample
++Increment (add 1)x++ (postfix), ++x (prefix)
--Decrement (subtract 1)x-- (postfix), --x (prefix)
+Unary plus (no change)+5 → 5
-Unary minus (negation)-5 → -5

Difference Between Prefix & Postfix:

  • ++x → First increments, then uses the value.
  • x++ → First uses the value, then increments.

Example:

int a = 5;  
cout << a++; // Prints 5, then a becomes 6  
cout << ++a; // First a becomes 7, then prints 7  

2. Assignment Operators

Assignment operators assign values to variables.

a. Simple Assignment (=)

A simple assignment stores a value in a variable.

int x = 10; // x now holds 10  

b. Compound Assignment (Shortcuts)

Combine arithmetic and assignment:

OperatorMeaningExampleEquivalent
+=Add & assignx += 5x = x + 5
-=Subtract & assignx -= 3x = x - 3
*=Multiply & assignx *= 2x = x * 2
/=Divide & assignx /= 2x = x / 2
%=Modulo & assignx %= 3x = x % 3

Why Use Them?

  • Shorter code.
  • Faster to write.

3. Comparison (Relational) Operators: Making Decisions

Compare two values and return true or false.

OperatorMeaningExample
==Equal to5 == 5 → true
!=Not equal5 != 3 → true
>Greater than5 > 3 → true
<Less than5 < 3 → false
>=Greater than or equal5 >= 5 → true
<=Less than or equal5 <= 3 → false

Used in:

  • if conditions.
  • Loops (whilefor).

Common Mistake:

  • Using = (assignment) instead of == (comparison).

4. Logical Operators: Combining Conditions

Combine multiple conditions:

OperatorMeaningExample
&&AND (both must be true)(5 > 3) && (2 < 4) → true
!NOT (reverse condition)!(5 == 3) → true

Short-Circuiting:

  • && stops if the first condition is false.
  • || stops if the first condition is true.

5. Bitwise Operators (Advanced – Optional)

Work on binary bits (0s and 1s).

OperatorMeaningExample
&Bitwise AND5 & 3 → 1
^Bitwise XOR5 ^ 3 → 6
~Bitwise NOT~5 → -6
<<Left shift5 << 1 → 10
>>Right shift5 >> 1 → 2

Uses:

  • Low-level programming.
  • Optimizing code.

Other Important Operators

OperatorMeaningExample
?:Ternary (short if-else)(x > 5) ? "Yes" : "No"
,Comma (separates expressions)x = (a=5, a+1) → x=6
::Scope resolutionstd::cout
. and ->Member accessobj.x or ptr->x
sizeofSize of variablesizeof(int) → 4 (bytes)

Operator Precedence and Associativity

Some operators run before others (like math: * before +).

Example:

int x = 5 + 3 * 2; // 3*2=6 → 5+6=11  

Common Precedence Rules

  1. Parentheses ()
  2. Unary (++--!~)
  3. Arithmetic (*/% → +-)
  4. Comparison (<><=>=)
  5. Equality (==!=)
  6. Logical (&& → ||)
  7. Assignment (=+=-=, etc.)

Associativity

  • Left-to-right: a + b + c → (a + b) + c
  • Right-to-left: x = y = 5 → y=5 then x=y

Using Parentheses for Clarity

Always use () to avoid confusion:

int result = (5 + 3) * 2; // 8 * 2 = 16  

Practical Examples

The following are practical examples of operators in C++ that clear all your confusion.

1. Simple Calculations

int a = 10, b = 3;  
cout << a + b; // 13  
cout << a % b; // 1  

2. Checking User Age

int age;  
cin >> age;  
if (age >= 18) {  
    cout << "Adult";  
} else {  
    cout << "Minor";  
}  

3. Looping with Operators

for (int i = 0; i < 10; i++) {  
    cout << i << " ";  
}  

Common Mistakes

  1. if (x = 5) → Wrong! (Use ==).
  2. 5 / 2 → 2 (Use 5.0 / 2 for 2.5).

About the Author

Picture of Muneeb Tariq

Muneeb Tariq

Muneeb Tariq is a Computer Science graduate and the founder of Educatecomputer. As a dedicated Computer Science Educator, he has dedicated himself to making technology simple and easy to understand for everyone. Muneeb takes complex technical topics and breaks them down into clear, straightforward lessons so that anyone can learn without feeling overwhelmed. His goal is to help people understand technology through honest and practical guidance, empowering them to confidently use digital tools in their daily lives.

Leave a Comment