Understanding Operators in C++

Understanding Operators in C++

8 mins read1.2K Views Comment
Updated on Jan 16, 2023 16:30 IST

Operators in C++perform mathematical and logical operations on entities or variables that are known as operands. The operators are represented by symbols that allow calculation between entities to produce relevant results.

2022_08_MicrosoftTeams-image-22-2.jpg

In the world of programming, an operator is a symbol that is used to perform operations on values known as operands. There are several types of operators in C++ to perform different operations. So, without further ado, let’s get started and discuss each type in detail.

We will be covering the following sections today:

C++ Arithmetic Operators

Arithmetic operators in C++ are mathematical functions that perform mathematical calculations on two values. These values are referred to as operands.

The C++ arithmetic operators are primarily of the following types –

Operator Name Syntax Explanation
+ Addition Operator a + b a = b + c
Subtraction Operator a – b a = a + b
* Multiplication Operator a * b a = a+ b
/ Division Operator a / b a = a * b
% Modulus Operator a % b a = a / b
++ Increment Operator ++ a a = a + 1
Decrement Operator — a a = a – 1

Let’s look at how each of these types is implemented in C++ through the following example:

 
#include
\n \n <iostream>\n \n
\n \n
using namespace std;\n \n
\n \n
int main() {\n \n
int a, b;\n \n
a = 17;\n \n
b = 3;\n \n
\n \n
//Addition\n \n
cout << "Sum: " << (a + b) << endl;\n \n
\n \n
//Subtraction\n \n
cout << "Difference: " << (a - b) << endl;\n \n
\n \n
//Multiplication\n \n
cout << "Product: " << (a * b) << endl;\n \n
\n \n
//Division\n \n
cout << "Quotient: " << (a / b) << endl;\n \n
\n \n
//Modulus\n \n
cout << "Remainder: " << (a % b) << endl;\n \n
\n \n
//Increment\n \n
int new_a = ++a;\n \n
cout << "Incremented a: " << new_a << endl;\n \n
\n \n
//Decrement\n \n
int new_b = --b;\n \n
cout << "Decremented b: " << new_b << endl;\n \n
\n \n
return 0;\n \n
}\n \n
\n \n </iostream>
Copy code

Output:

 
Sum: 20
Difference: 14
Product: 51
Quotient: 5
Remainder: 2
Incremented a: 18
Decremented b: 2
Copy code

C++ Assignment Operators

Assignment operators in C++ are used to assign values to variables. These operators assign the right-hand side operand value to the left-hand side operand. So, assignment operators are basically a variation of the = symbol.

The following table describes various C++ assignment operators –

Operator Name Syntax Equivalent to:
= Simple Assignment Operator a = (b+c) a = b + c
+= Addition Assignment Operator a += b a = a + b
-= Subtraction Assignment Operator a -= b a = a+ b
*= Multiplication Assignment Operator a *= b a = a * b
/= Division Assignment Operator a /= b a = a / b
%= Modulus Assignment Operator a %= b a = a %  b

Let’s look at how each of these types is implemented in C++ through the following example:

 
#include
\n \n <iostream>\n \n
\n \n
using namespace std;\n \n
\n \n
int main() {\n \n
int a, b;\n \n
\n \n
// Simple Assignment Operator\n \n
a = 13;\n \n
b = 9;\n \n
\n \n
cout << "Value of a: " << a << endl;\n \n
cout << "Value of b: " << b << endl;\n \n
\n \n
//Addition Assignment Operator\n \n
a += b;\n \n
cout << "New Value of a: " << a << endl;\n \n
\n \n
//Subtraction Assignment Operator\n \n
a -= b;\n \n
cout << "New Value of a: " << a << endl;\n \n
\n \n
//Multiplication Assignment Operator\n \n
a *= b;\n \n
cout << "New Value of a: " << a << endl;\n \n
\n \n
//Division Assignment Operator\n \n
a /= b;\n \n
cout << "New Value of a: " << a << endl;\n \n
\n \n
//Modulus Assignment Operator\n \n
a %= b;\n \n
cout << "New Value of a: " << a << endl;\n \n
\n \n
return 0;\n \n
}\n \n
\n \n </iostream>
Copy code

Output:

 
Value of a: 13
Value of b: 9
New Value of a: 22
New Value of a: 13
New Value of a: 117
New Value of a: 13
New Value of a: 4
Copy code

C++ Relational Operators

Relational operators in C++ are used to check the relationship between operands and perform comparisons between them.

The following table describes various C++ relational operators –

Operator Name Syntax Explanation:
== Equal to Operator a == b Checks the equality of both operands.
!= Not equal to Operator a != b Checks inequality of both operands.
Greater than Operator a > b Checks if the left operand is greater than the right operand.
Lesser than Operator a < b Checks if the left operand is lesser than the right operand.
>= Greater than or equal to Operator a >= b Checks if the left operand is greater than or equal to the right operand.
<= Lesser than or equal to Operator a <= b Checks if the left operand is lesser than or equal to the right operand.

Let’s look at how each of these types is implemented in C++ through the following example:

 
#include
\n \n <iostream>\n \n
\n \n
using namespace std;\n \n
\n \n
int main() {\n \n
int a, b;\n \n
a = 6;\n \n
b = 12;\n \n
bool result;\n \n
\n \n
result = (a == b); // false\n \n
cout << "6 == 12 is " << result << endl;\n \n
\n \n
result = (a != b); // true\n \n
cout << "6 != 12 is " << result << endl;\n \n
\n \n
result = a > b; // false\n \n
cout << "6 > 12 is " << result << endl;\n \n
\n \n
result = a < b; // true\n \n
cout << "6 < 12 is " << result << endl;\n \n
\n \n
result = a >= b; // false\n \n
cout << "6 >= 12 is " << result << endl;\n \n
\n \n
result = a <= b; // true\n \n
cout << "6 <= 12 is " << result << endl;\n \n
\n \n
return 0;\n \n
}\n \n
\n \n </iostream>
Copy code

Output:

 
6 == 12 is 0
6 != 12 is 1
6 > 12 is 0
6 < 12 is 1
6 >= 12 is 0
6 <= 12 is 1
Copy code

In the above example, result=1 represents Boolean True, and result=0 represents Boolean False. So, whenever the condition is true, the result is 1 and 0 when the condition is false.

Explore free C++ courses

C++ Logical Operators

Logical operators in C++ are used to check the operands (expressions or conditions). If the condition(s) hold true, the output is 1 whereas if the condition is false, it returns 0. These operators are mostly used for decision-making in C++.

The following table describes various C++ logical operators –

Operator Name Explanation:
&& Logical AND Operator Returns True if all operands are true.
|| Logical OR Operator Returns True if at least one of the operands is true.
! Logical NOT Operator Reverses the operands’ logical state – converts true to false and vice-versa.

Let’s understand through an example. We will take two variables as shown below:

 
a = 6
b = 9
(a > 4) && (b > 6) evaluates to true
(a > 4) && (b < 6) evaluates to false
(a > 4) || (b > 6) evaluates to true
(a > 4) || (b < 6) evaluates to true
(a < 4) || (b < 6) evaluates to false
!(a < 4) evaluates to true
!(a > 4) evaluates to false
Copy code

Now let’s look at how these operators are implemented in C++ through the following example:

 
#include
\n \n <iostream>\n \n
\n \n
using namespace std;\n \n
\n \n
int main() {\n \n
bool result;\n \n
\n \n
result = (4 != 6) && (4 < 6); // true\n \n
cout << "(4 != 6) && (4 < 6) is " << result << endl;\n \n
\n \n
result = (4 == 6) && (4 < 6); // false\n \n
cout << "(4 == 6) && (4 < 6) is " << result << endl;\n \n
\n \n
result = (4 == 6) && (4 > 6); // false\n \n
cout << "(4 == 6) && (4 > 6) is " << result << endl;\n \n
\n \n
result = (4 != 6) || (4 < 6); // true\n \n
cout << "(4 != 6) || (4 < 6) is " << result << endl;\n \n
\n \n
result = (4 != 6) || (4 > 6); // true\n \n
cout << "(4 != 6) || (4 > 6) is " << result << endl;\n \n
\n \n
result = (4 == 6) || (4 > 6); // false\n \n
cout << "(4 == 6) || (4 > 6) is " << result << endl;\n \n
\n \n
result = !(6 == 3); // true\n \n
cout << "!(6 == 3) is " << result << endl;\n \n
\n \n
result = !(6 == 6); // false\n \n
cout << "!(6 == 6) is " << result << endl;\n \n
\n \n
return 0;\n \n
}\n \n
\n \n </iostream>
Copy code

Output:

 
(4 != 6) && (4<6) is 1
(4 == 6) && (4<6) is 0
(4 == 6) && (4>6) is 0
(4 != 6) || (4<6) is 1
(4 != 6) || (4>6) is 1
(4 == 6) || (4>6) is 0
!(6 == 3) is 1
!(6 == 6) is 0
Copy code
Understanding Function Overriding in C++
Understanding Function Overriding in C++
A derived class inherits the features of base class. If the same function is defined in both base and derived class and we call the function through the object of...read more
All About C++ Friend Function
All About C++ Friend Function
Friend function in C++ is a non-member function of a class. It is defined outside the class scope but it does have the access to a private data member of...read more
Understanding Break Statement in C++
Understanding Break Statement in C++
The break statement is loop control statement that is used for the terminating the loop. These are used in the situations where we do not have the idea about actual...read more

C++ Bitwise Operators

Bitwise operators in C++ are used to perform operations on operands treating them as a string of bits. Hence, the name bitwise – stands for bit-by-bit operation. The returned output is in decimal format. They can also be used alongside char and int data types.

The following table describes various C++ bitwise operators –

Operator Name Explanation:
& Logical AND Operator Takes 2 operands, then performs AND on each bit of the two operands. If both are 1, AND returns 1, otherwise 0.
| Logical OR Operator Takes 2 operands, then performs OR on every bit of two operands. It returns 1 if one of the bits is 1.
^ Bitwise XOR Operator Takes 2 operands, then performs XOR on every bit of 2 numbers. It returns 1 if both bits are different.
~ Bitwise One’ Complement Takes an operand, then inverts all its bits.
<<  Binary Left Shift Operator Takes two operands, then left shifts the bits of the first operand. The second operand determines the total places to shift.
>>  Binary Right Shift Operator Takes two operands, then right shifts the bits of the first operand. The second operand determines the total places to shift.

Now let’s look at how these operators are implemented in C++ through the following example:

 
#include
\n \n <iostream>\n \n
\n \n
using namespace std;\n \n
\n \n
int main() {\n \n
unsigned int a = 60; // 60 = 0011 1100 \n \n
unsigned int b = 13; // 13 = 0000 1101\n \n
int c = 0;\n \n
\n \n
c = a & b;\n \n
cout << "a & b: " << c << endl; // 12 = 0000 1100\n \n
\n \n
c = a | b;\n \n
cout << "a | b: " << c << endl; // 61 = 0011 1101\n \n
\n \n
c = a ^ b;\n \n
cout << "a ^ b: " << c << endl; // 49 = 0011 0001\n \n
\n \n
c = ~a;\n \n
cout << "~a: " << c << endl; // -61 = 1100 0011\n \n
\n \n
c = a << 2;\n \n
cout << "a << 2: " << c << endl; // 240 = 1111 0000\n \n
\n \n
c = a >> 2;\n \n
cout << "a >> 2: " << c << endl; // 15 = 0000 1111\n \n
\n \n
return 0;\n \n
}\n \n
\n \n </iostream>
Copy code

Output:

 
a & b: 12
a | b: 61
a ^ b: 49
~a: -61
a<<2: 240
a>>2: 15
Copy code

Other Operators in C++

Size of Operator

The sizeof operator is used for the determination of the size of a variable having a particular data type. The following example illustrates how this operator is applied to the most common data types in C++:

 
#include
\n \n <iostream>\n \n
\n \n
using namespace std;\n \n
int main() {\n \n
cout<<"Size of int : "<< sizeof(int) << "\n";\n \n
\n \n
cout<<"Size of char : " << sizeof(char) << "\n";\n \n
\n \n
cout<<"Size of float : " << sizeof(float) << "\n";\n \n
\n \n
cout<<"Size of double : " << sizeof(double) << "\n";\n \n
\n \n
return 0;\n \n
}\n \n
\n \n </iostream>
Copy code

Output:

 
Size of int: 4
Size of char: 1
Size of float: 4
Size of double: 8
Copy code

Conditional Operator

The conditional operator, aka the ternary operator in C++, represented by (? ???? is used to evaluate a test condition and implement a block of code based on the outcome of the evaluation.

Syntax:

Condition ? TestExpression1 : TestExpression2;

Here, the Condition is evaluated:

  • If Condition is true, TestExpression1 is executed.
  • And, if Condition is false, TestExpression2 is executed.

Now let’s look at how this operator is implemented in C++ through the following example:

 
#include
\n \n <iostream>\n \n
\n \n
using namespace std;\n \n
int main() {\n \n
int a = 1;\n \n
int b;\n \n
\n \n
//conditional operator\n \n
b = (a < 10) ? 2 : 5;\n \n
\n \n
//display result\n \n
cout << "The value of b is: " << b << endl;\n \n
return 0;\n \n
}\n \n
\n \n </iostream>
Copy code

Output:

 
The value of b is: 2
Copy code

Comma Operator

The comma operator, represented by (,) is used to trigger the performance of a sequence of operations. It expresses the first operand and discards the result. After that, it evaluates the second operand and returns the value and type.

Now let’s look at how this operator is implemented in C++ through the following example:

 
#include
\n \n <iostream>\n \n
\n \n
using namespace std;\n \n
int main() {\n \n
int a, b;\n \n
b = 100;\n \n
\n \n
//comma operator\n \n
a = (b++, b + 10, 99 + b);\n \n
\n \n
//display output\n \n
cout << a;\n \n
return 0;\n \n
}\n \n
\n \n </iostream>
Copy code

Output:

 
200
Copy code

Operator Precedence in C++

What happens when there are multiple operators at work in a single expression? For example, say we need to calculate  x = 5 + 3 * 2;

Here, the value of x is 11, not 16 because operator * has higher precedence than operator +.

Hence, 3 * 2 is calculated first, and the result is then added to 5.

The order of precedence determines how operators are grouped in the absence of parentheses in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others. The following table illustrates the precedence of operators in C++:

Type of Operator Operator Precedence
Postfix (), [], ->, ++, – – Left to right
Unary +, -, !, ~, ++, –, &, sizeof Right to left
Multiplicative *, /, %, Left to right
Additive +, – Left to right
Shift <<, >> Left to right
Relational <<=, >>= Left to right
Equality ==, != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ? : Right to left
Assignment =, +=, -=, *=, /=, %=,>>=, <<=, &=, ^=, |= Right to left
Comma , Left to right

 Endnotes

Through this article, you will learn about types of operators in C++ and how they are implemented. We also learned about the precedence of operators. If you wish to learn more about C++ and solidify your basics, you can explore our articles on the C++.

______________________

Recently completed any professional course/certification from the market? Tell us what you liked or disliked in the course for more curated content.

Click here to submit its review with Shiksha Online

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio