Home » Operators in C++

Operators in C++6 min read

by TechABU
Operators in C++

Operators are very important to use in C++ because without their use, expressions cannot be evaluated.

With the help of examples, we will learn about the various types of operators in C++ in this article. 

An operator is a symbol in programming that operates on a value or variable. In simple words, an operator performs operations on operands. The operation might be arithmetic or data comparison.

For example;

x = a+b;

In the above example, a, b, and x are operands and “+” and “=” are the operators.

Operators in C++ can be classified into:

  1. Assignment Operators
  2. Arithmetic Operators
  3. Compound Operators
  4. Relational Operators
  5. Increment/decrement Operators
  6. Logical Operator

#1. Assignment Operators:

The assignment operator assigns a value to a variable. The symbol “=” is used as an assignment operator.

For example;

x = 26;

The integer 26 is assigned to the variable x in the above statement. The left side of the assignment operator “=” is a variable, while the right side can be a constant value, a variable, an operation result, or a combination of these.

The assignment operation is always performed from right to left.

#2. Arithmetic Operators:

(+, -, *, /, %) are the five arithmetic operators used in C++ that support the following arithmetic operations.

Arithmetic Operators in C++

The percentage sign “%” shown in the above table is used for modulus. The remainder of a division of two values is determined by modulus.

For example;

Remainder = 11 % 5;

The variable “remainder” will contain the value 1 since 1 is the remainder from dividing by 5. 

#3. Compound Assignment Operators:

(+=, -=, *=, /=, %=) are the compound assignment operators, also known as arithmetic assignment operators used in C++.

In C++, these operators are used to modify the value of a variable by executing an operation on the value currently stored in the variable.

For example;


The preceding code will be used to add the value of a variable b to the value of another variable a.

To make it more simple, consider the code like this:

Compound Operators in C Plus Plus

Consider the following program to understand the concept of Compound Assignment Operators in C++.

// demonstrates arithmetic assignment operators
#include <iostream>
using namespace std;

int main()
    int ans = 27;

    ans += 10;          //same as: ans = ans + 10;
    cout << ans;
    return 0;


Compound Operator example output

#4. Relational Operators:

Relational operators compare two expressions or, to put it another way, they check the relationship between two operands.

These operators can provide either true or false results.

For example;

10 > 20;

The above single line statement check if 10 is greater than 20. Here, “>” is a relational operator. It checks whether 10 is greater than 20.

If the relationship is true, it returns 1, but if it is false, it returns 0.

Relational Operators in C++

Consider the following program to understand the concept of Relational Operators in C++.

//A CPP Program to demonstrate the use of Relational operators

#include <iostream>
using namespace std;

int main()

	int marks;
	cout<<"Enter Your Marks= "<< endl;
	cin>> marks;
	return 0;


Relational Operator example output

#5. Increment/decrement Operators:

increment/decrement operators are also known as Unary arithmetic operators. These operators are used to shorten the length of expressions.

The increment and decrement operators “++” and “–” respectively increase and decrease the value stored in a variable by 1.

For example;

Var1 = Var + 1;
Var1 += 1;

The above statements are similar, and they all increase the value of the variable “Var1” by 1.

To decrease the value of the variable “Var1”, you just need to change the “+” sign to “-.”

For example;

Var1 = Var - 1;
Var1 -= 1;

Each increment and decrement operator has two versions—the prefix and postfix versions.

The operators precede their operands in the prefix form of the increment and decrement operators, e.g., ++Var1, –Var1.

While in postfix form, the increment and decrement operators appear in the expression after the operands, e.g., Var1++, Var1–.

Consider the following program to understand the concept of Increment/Decrement Operators in C++.

//increment/decrement operator example

#include <iostream>

using namespace std;

   int x = 5;
   cout<<"Increment in prefix form ="<<++x<<endl;
   cout<<"Decrement In Prefix Form ="<<--x<<endl;
   return 0;

#6. Logical Operators:

Logical operators are mostly used in decision-making statements. To evaluate if an expression is true or false, logical operators are used. If the expression is true, it will return 1, but if it is false, it will return 0.

There are three logical operators used in C++. These are:

  1. Logical NOT ( ! )
  2. Logical AND ( && )
  3. Logical OR ( || )
Logical Operators in C++

#1. Logical NOT

The ! operator is the C++ unary operator that is used to perform the Boolean NOT operation. The output of this operator is the inverse of the value of its operand.

Logical NOT Operator In C++

It produces false if its operand is true and true if its operand is false.

#2. Logical AND

The logical “&&” operator is a two-operand binary operator. It is used to check whether or not both conditions are true.

Logical AND Operator in C++

The logical “&&” returns true if both conditions are true; otherwise, it returns false.

#3. Logical OR

The logical operator ” || ” is used to check if two conditions are true. The logical ” || ” operator returns true if either the left operand or the right operand evaluates to true.

Logical OR Operator in C++

Also, if both the operands are true, logical “ || “ returns to true, but if both are false, it returns to false.

Consider the following program to understand the concept of Logical Operators in C++.

// C++ program to demonstrate working of
// logical operators

#include <iostream>
using namespace std;

int main()
	int a = 20, b = 40, c = 10, d = 70;

	// logical operators

	// logical AND example
	if (a > b && c == d) 
		cout << "a is greater than b AND c is equal to d\n";
		cout << "AND condition not satisfied\n";

	// logical OR example
	if (a > b || c == d)
		cout << "a is greater than b OR c is equal to d\n";
		cout << "Neither a is greater than b nor c is equal "
				" to d\n";

	return 0;


Logical Operators example output

Unary, Binary, and Ternary Operators:

Unary, binary, and ternary operators are the three primary categories of C++ operators.

This division is based on the number of operands that the operators operate on.

#1. Unary Operators:

Unary operators are those that only require one operand. There are two unary operators in arithmetic: unary + and unary -.

For example;

a = +x;
b = -y;
c = !y;

The logical NOT ( ! ) and the increment/decrement operators are examples of unary operators.

#2. Binary Operators:

The binary operators are those that need two operands. Example of binary operators are (+, -, *, /, %).

The Assignment Operator ( = ) is also known as a binary operator as it also requires two operands.

In simple words, all operators that require two operands are known as binary operators.

#3. Ternary Operators:

Three operands are required for the ternary operator. Only one ternary operator exists in a statement. The operator (?:) is a conditional operator.

Consider the following example of the Ternary operator.

//A CPP Program to find the greater number out of 3 using Ternary Operator

#include <iostream>
#include <conio.h>
using namespace std;

int main()

	int num1, num2, num3, max;
	cout<< "Enter Three Numbers= "<< endl;
	cin>> num1 >> num2 >> num3 ;
	max = num1;
	max= (num2>max)?num2:max;
	max= (num3>max)?num3:max;
	cout<< "The Max Integer is ="<< max<<endl;


Ternary Operator example output

Leave a Comment


Send this to a friend