C++ Operators

Operators are one of the most important parts of C++. They are used to perform different operations on variables and values. Whether you want to add two numbers, compare values, check conditions, or perform calculations, you will use operators.

For example, if you write 10 + 20, the + symbol is an operator that adds two values together. Similarly, >, ==, and && are also operators that perform different tasks.

In this tutorial, you will learn about Arithmetic Operators in C++, assignment operators, Relational Operators in C++, Logical Operators in C++, increment and decrement operators, bitwise operators, the conditional operator, Operator Precedence in C++, and associativity.


Arithmetic Operators in C++

Arithmetic operators are used to perform mathematical calculations such as addition, subtraction, multiplication, and division.

OperatorDescription
+Addition
-Subtraction
*Multiplication
/Division
%Modulus (Remainder)

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;
}

Arithmetic operators are used in almost every C++ program because calculations are a common part of programming.


Assignment Operators

An assignment operator is used to assign a value to a variable.

The most commonly used assignment operator is =.

Example

int marks = 90;

Here, the value 90 is assigned to the variable marks.

C++ also provides shorthand assignment operators.

OperatorExample
+=a += 5
-=a -= 5
*=a *= 5
/=a /= 5
%=a %= 5

These operators make the code shorter and easier to read.


Relational Operators in C++

Sometimes you need to compare two values. For example, checking whether one number is greater than another or whether two values are equal. This is where Relational Operators in C++ are used.

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

Example

int a = 20;
int b = 15;

cout << (a > b);

Since 20 is greater than 15, the output will be 1, which means true.

Relational operators always return either true (1) or false (0).


Logical Operators in C++

Logical operators are used when you want to check more than one condition at the same time.

The three logical operators are:

OperatorMeaning
&&AND
`
!NOT

Example

int age = 22;

cout << (age >= 18 && age <= 60);

Here, both conditions are true, so the result will also be true.

Logical operators are commonly used with if, while, and other control statements.


Increment and Decrement Operators

The increment operator (++) increases a variable’s value by 1, while the decrement operator (--) decreases it by 1.

Example

int x = 10;

x++;
cout << x;

Output

11

Similarly,

x--;

reduces the value by one.

These operators are frequently used inside loops.


Bitwise Operators

Bitwise operators work on the binary form of numbers. They are mainly used in system programming, embedded systems, and performance-related applications.

Some common bitwise operators are:

OperatorMeaning
&Bitwise AND
``
^Bitwise XOR
~Bitwise NOT
<<Left Shift
>>Right Shift

If you are a beginner, don’t worry if these operators look confusing. You will understand them better after learning binary numbers.


Conditional Operator

The conditional operator (?:) is also known as the ternary operator. It is a shorter way of writing a simple if...else statement.

Syntax

condition ? value1 : value2;

Example

int age = 18;

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

If the condition is true, "Eligible" is printed. Otherwise, "Not Eligible" is displayed.


Operator Precedence in C++

Sometimes a statement contains more than one operator.

For example:

int result = 10 + 5 * 2;

Will addition happen first or multiplication?

C++ follows a set of rules called Operator Precedence. Operators with higher precedence are evaluated before operators with lower precedence.

In the above example, multiplication is performed first.

10 + (5 × 2)

So, the final answer is 20, not 30.

Using brackets () is always a good practice because they make your code easier to understand.


Associativity

What happens if two operators have the same precedence?

In that case, C++ follows another rule called associativity. It decides whether the expression is evaluated from left to right or right to left.

For example:

int result = 20 - 10 - 5;

Subtraction has left-to-right associativity.

So, C++ evaluates it like this:

(20 - 10) - 5

The final answer is 5.

Understanding precedence and associativity helps you write correct expressions and avoid unexpected results.


Frequently Asked Questions (FAQs)

Q1. What are the different types of operators in C++?

C++ Operators include arithmetic, assignment, relational, logical, bitwise, increment/decrement, and conditional operators. Each type performs a specific operation on data.

Q2. What is the difference between arithmetic and logical operators in C++?

Arithmetic Operators in C++ perform mathematical calculations, while Logical Operators in C++ evaluate conditions and return true or false.

Q3. Why is operator precedence important in C++?

Operator Precedence in C++ determines the order in which operators are evaluated. Understanding it helps you write correct expressions and avoid unexpected results.

Q4. How can I change operator precedence in C++?

You can change the evaluation order by using parentheses (). Expressions inside parentheses are executed before other operators.

Q5. What is the difference between prefix and postfix increment operators?

The prefix operator (++x) increments a value before it is used, whereas the postfix operator (x++) uses the current value first and increments it afterward.

Q6. Which C++ operators are used most frequently?

Arithmetic, assignment, relational, and logical operators are the most commonly used C++ Operators because they are essential for calculations, comparisons, and decision-making.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top