Back to C++
2026-05-075 min read

operator (C++)

Learn operator (C++) step by step with clear examples and exercises.

Why This Matters

Operators are essential components in C++ programming that allow you to manipulate data, perform calculations, and make decisions. Understanding operators is crucial for writing efficient, effective, and easy-to-understand code. This guide delves into the world of operators, providing practical examples and real-world scenarios where they are used. Mastering operators will help you solve complex problems, debug issues, and prepare for interviews.

Why Operators Matter in C++ Programming

Operators play a significant role in C++ programming as they allow you to:

  1. Perform mathematical calculations (arithmetic operators)
  2. Compare values (relational operators)
  3. Combine conditional statements (logical operators)
  4. Manipulate the individual bits of binary representations (bitwise operators)
  5. Assign values to variables (assignment operators)
  6. Increment or decrement a variable's value (unary operators)
  7. Determine the size of a data type or variable (sizeof operator)

Understanding operators is essential for writing clean, efficient code that can solve complex problems and help you excel in interviews.

Prerequisites

Before diving into the core concept of operators, it's essential to have a solid understanding of:

  • Basic C++ concepts (variables, data types, input/output)
  • Control structures (if statements, loops)
  • Understanding of basic mathematical operations and logical reasoning
  • Familiarity with C++ syntax and the standard library

Basic Mathematical Operations

Before we dive into operator examples in C++, let's review some fundamental mathematical operations:

  1. Addition (+): Combines two or more numbers to produce a result.
  2. Subtraction (-): Removes one number from another.
  3. Multiplication (*): Multiplies two numbers together.
  4. Division (/): Divides one number by another.
  5. Modulus (%): Returns the remainder of the division operation.
  6. Increment (++): Increases a variable's value by 1.
  7. Decrement (--): Decreases a variable's value by 1.
  8. Exponentiation (): Raises one number to the power of another.
  9. Remainder operator for fractional numbers (fmod): Returns the remainder of the division operation for fractional numbers.

Core Concept

Operators in C++ can be broadly categorized as follows:

Arithmetic Operators

These operators are used for mathematical operations like addition, subtraction, multiplication, division, modulus, exponentiation, and remainder operator for fractional numbers.

int a = 5;
int b = 3;
float c = 2.5f;

// Arithmetic operations
int sum = a + b; // Addition
double avg = (a + b + c) / 3.0; // Average
int product = a * b; // Multiplication
double quotient = (double)a / b; // Division
int remainder = a % b; // Modulus
double power = pow(a, 2); // Exponentiation
float fractionalRemainder = fmod(c, 1.0f); // Remainder for fractional numbers
a++; // Increment

Relational Operators

Relational operators compare two operands and return a boolean value (true or false). They include equal to (==), not equal to (!=), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=).

int x = 10;
int y = 20;
float z = 3.5f;

// Relational operations
bool isEqual = x == y; // Equal to
bool isNotEqual = x != y; // Not equal to
bool isGreater = x > y; // Greater than
bool isLess = x < y; // Less than
bool isGreaterOrEqual = x >= y; // Greater than or equal to
bool isLessOrEqual = x <= y; // Less than or equal to
bool isGreaterThanZ = z > 3.0f; // Comparing with a float

Logical Operators

Logical operators are used to combine conditional statements. They include logical AND (&&), logical OR (||), and logical NOT (!).

int a = 5;
int b = 10;
bool c = true;

// Logical operations
bool isBiggerAndEven = (b > 5) && (b % 2 == 0); // Logical AND
bool isAOrBGreaterThanTen = (a > 10) || (b > 10); // Logical OR
bool isNotBigger = ! (b > 5); // Logical NOT

Bitwise Operators

Bitwise operators manipulate the individual bits of binary representations. They include bitwise AND (&), bitwise OR (|), bitwise XOR (^), bitwise NOT (~), left shift (<<), and right shift (>>).

int a = 60; // binary: 1111000
int b = 13; // binary: 00001101

// Bitwise operations
int result = a & b; // binary AND: 00000000 (0)
int result2 = a | b; // binary OR: 11111101 (61)

Assignment Operators

Assignment operators are used to assign values to variables. They include simple assignment (=), compound assignment (+=, -=, *=, /=, %=, etc.).

int a = 5;
a += 3; // equivalent to: a = a + 3;

Unary Operators

Unary operators operate on a single operand. They include increment (++), decrement (--), reference (&), dereference (*), and address-of (&).

int a = 5;
int b = ++a; // pre-increment: a becomes 6, b gets the new value of a

Sizeof Operator

The sizeof operator returns the size (in bytes) of a data type or variable.

int a = 5;
size_t size = sizeof(a); // size is 4 bytes on most systems

Worked Example

In this example, we will create a simple program that calculates the factorial of a number using various operators.

#include <iostream>
using namespace std;

int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}

int main() {
int num = 5;
cout << "Factorial of " << num << " is: " << factorial(num);
return 0;
}

Common Mistakes

  1. Forgetting the assignment operator (=) when using compound assignment operators (+=, -=, etc.)
  2. Using relational operators instead of equality operators (== vs =) in conditional statements
  3. Ignoring the order of precedence between different types of operators
  4. Not understanding bitwise operations and their effects on binary representations
  5. Using unary operators incorrectly or forgetting the pre-increment/decrement difference
  6. Misusing assignment operators when intended for comparison (e.g., a = b instead of a == b)
  7. Confusing bitwise AND (&) with logical AND (&&)
  8. Forgetting to initialize variables before using them in expressions
  9. Using the wrong operator for exponentiation (^ vs )
  10. Not considering the signedness of integers when performing arithmetic operations
  11. Not handling overflow and underflow errors when working with large numbers
  12. Forgetting to include necessary header files (e.g., for mathematical functions like pow())

Practice Questions

  1. Write a C++ program that calculates the factorial of a number using various operators.
  2. Given two integers, write a program to check if they are equal or not using relational operators.
  3. Write a program to find the largest among three numbers using C++ operators.
  4. Write a function that swaps two variables without using a temporary variable using bitwise XOR operator.
  5. Given an integer n, write a program to check if it is even or odd using bitwise AND operator.
  6. Write a program to find the sum of all numbers from 1 to 100 using arithmetic operators.
  7. Write a program that calculates the area and perimeter of a rectangle with length l and width w. Use C++ operators for calculations.

FAQ

What is an operator in C++?

An operator in C++ is a symbol or character that performs specific mathematical, logical, or assignment operations on operands (values or variables).

How many types of operators are there in C++?

There are six main categories of operators in C++: arithmetic, relational, logical, bitwise, assignment, and unary.

What is the difference between = and == in C++?

The = operator assigns a value to a variable, while the == operator compares two values for equality.

How does the sizeof operator work in C++?

The sizeof operator returns the size (in bytes) of a data type or variable in memory.

What is the difference between pre-increment and post-increment operators in C++?

Pre-increment (++a) increments the value of the variable before it is used in an expression, while post-increment (a++) increments the value after it is used.

operator (C++) | C++ | XQA Learn