C++ Operators


What Are Operators in C++?

Operators in C++ are special symbols used to perform operations on variables and values. They help manipulate data, perform calculations, compare results, and control program logic. In short, operators are the foundation of all computations in C++.

For example:

int sum = 10 + 5;

Here, + is an operator that adds two numbers.

Operators work with operands (the values they act upon). In the above example, 10 and 5 are operands, while + is the operator.

Types of Operators in C++

C++ provides a wide variety of operators grouped into several categories based on their function. The main types are:

  1. Arithmetic Operators

  2. Assignment Operators

  3. Comparison (Relational) Operators

  4. Logical Operators

  5. Increment and Decrement Operators

  6. Bitwise Operators

  7. Conditional (Ternary) Operator

  8. Sizeof and Comma Operators

Let’s go through each type in detail with examples.

1. Arithmetic Operators in C++

Arithmetic operators are used to perform mathematical operations.

Operator Description Example
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
% Modulus (Remainder) a % b

Example:

#include <iostream>
using namespace std;

int main() {
    int a = 15, b = 4;
    cout << "Addition: " << a + b << endl;
    cout << "Subtraction: " << a - b << endl;
    cout << "Multiplication: " << a * b << endl;
    cout << "Division: " << a / b << endl;
    cout << "Remainder: " << a % b;
    return 0;
}

Output:

Addition: 19
Subtraction: 11
Multiplication: 60
Division: 3
Remainder: 3

2. Assignment Operators in C++

Assignment operators are used to assign values to variables.

Operator Example Same As
= x = 5 Assigns 5 to x
+= x += 3 x = x + 3
-= x -= 2 x = x - 2
*= x *= 4 x = x * 4
/= x /= 5 x = x / 5
%= x %= 3 x = x % 3

Example:

int a = 10;
a += 5; // a = 15
a *= 2; // a = 30

Assignment operators are especially useful when you want to perform operations and assign results simultaneously.

3. Comparison (Relational) Operators

These operators are used to compare two values and return a boolean result (true or false).

Operator Description Example
== Equal to a == b
!= Not equal to a != b
> Greater than a > b
< Less than a < b
>= Greater than or equal to a >= b
<= Less than or equal to a <= b

Example:

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

Relational operators are mainly used in conditions and loops to make decisions.

4. Logical Operators in C++

Logical operators combine multiple conditions. They are mostly used in if statements and loops.

Operator Description Example
&& Logical AND (a > 5 && b < 10)
`   `
! Logical NOT !(a == b)

Example:

int a = 7, b = 3;
if (a > 5 && b < 5) {
    cout << "Both conditions are true";
}

5. Increment and Decrement Operators

These operators increase or decrease the value of a variable by 1.

Operator Description Example
++ Increment ++x or x++
-- Decrement --x or x--

Example:

int x = 10;
cout << ++x; // Pre-increment, x becomes 11 before printing
cout << x++; // Post-increment, prints 11 then x becomes 12

6. Bitwise Operators

Bitwise operators work on bits rather than entire numbers. They are commonly used in low-level programming and embedded systems.

Operator Description Example
& AND a & b
` ` OR
^ XOR a ^ b
~ NOT ~a
<< Left shift a << 1
>> Right shift a >> 1

Example:

int a = 5;   // Binary: 0101
int b = 3;   // Binary: 0011
cout << (a & b); // 1

7. Conditional (Ternary) Operator

The ternary operator ?: is a shorthand way of writing an if-else statement.
Syntax:

condition ? expression1 : expression2;

Example:

int age = 18;
string result = (age >= 18) ? "Eligible" : "Not Eligible";
cout << result;

Output:

Eligible

8. Sizeof and Comma Operators

sizeof Operator

Used to determine the size of a data type or variable.

cout << sizeof(int);  // Output: 4

Comma Operator

Used to separate multiple expressions that are evaluated from left to right.

int a = (2, 4, 6);
cout << a; // Output: 6

The comma operator returns the value of the last expression.

Operator Precedence and Associativity

When multiple operators appear in a single expression, precedence determines which operator is evaluated first, while associativity defines the direction of evaluation (left to right or right to left).

For example:

int result = 10 + 5 * 2; // result = 20, not 30

Here, multiplication has higher precedence than addition.

Operator Type Associativity
Arithmetic (*, /, %) Left to Right
Arithmetic (+, -) Left to Right
Relational (<, >, <=, >=) Left to Right
Logical (&&, `  
Assignment (=, +=, -=) Right to Left

Summary of C++ Operators

Operators in C++ are essential tools for performing mathematical, logical, and comparison operations. From simple arithmetic to bitwise manipulations, they form the basis of every program. Understanding how each operator works — along with precedence and associativity — helps you write efficient and bug-free code. Mastering operators allows you to build logical expressions and implement real-world problem-solving easily in C++.


Practice Questions

  1. What are operators in C++ and how do they work with operands?

  2. Write a C++ program to perform all arithmetic operations (addition, subtraction, multiplication, division, and modulus) on two integers.

  3. What is the difference between assignment and comparison operators? Give examples of each.

  4. Explain the working of logical operators in C++ with an example using if conditions.

  5. Write a C++ program to take two numbers as input and display which one is greater using a relational operator.

  6. What is the difference between pre-increment (++x) and post-increment (x++)? Show with a short code example.

  7. Write a C++ program that uses the ternary operator to find the largest of two numbers.

  8. What are bitwise operators? Write a program to demonstrate the use of &, |, and ^ operators.

  9. Explain the concept of operator precedence and associativity with an example.

  10. Write a program that uses both assignment and arithmetic operators to calculate the average of three numbers.


Try a Short Quiz.

coding learning websites codepractice

No quizzes available.

Go Back Top