Understanding Operators in C++
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.
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
- C++ Assignment Operators
- C++ Relational Operators
- C++ Logical Operators
- C++ Bitwise Operators
- Other Operators in C++
- Operator Precedence in C++
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 \n <iostream>\n \n \n \n \n \n using namespace std;\n \n \n \n \n \n int main() {\n \n \n int a, b;\n \n \n a = 17;\n \n \n b = 3;\n \n \n \n \n \n //Addition\n \n \n cout << "Sum: " << (a + b) << endl;\n \n \n \n \n \n //Subtraction\n \n \n cout << "Difference: " << (a - b) << endl;\n \n \n \n \n \n //Multiplication\n \n \n cout << "Product: " << (a * b) << endl;\n \n \n \n \n \n //Division\n \n \n cout << "Quotient: " << (a / b) << endl;\n \n \n \n \n \n //Modulus\n \n \n cout << "Remainder: " << (a % b) << endl;\n \n \n \n \n \n //Increment\n \n \n int new_a = ++a;\n \n \n cout << "Incremented a: " << new_a << endl;\n \n \n \n \n \n //Decrement\n \n \n int new_b = --b;\n \n \n cout << "Decremented b: " << new_b << endl;\n \n \n \n \n \n return 0;\n \n \n }\n \n \n \n \n \n </iostream>
Output:
Sum: 20Difference: 14Product: 51Quotient: 5Remainder: 2Incremented a: 18Decremented b: 2
Best-suited C++ courses for you
Learn C++ with these high-rated online courses
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 \n <iostream>\n \n \n \n \n \n using namespace std;\n \n \n \n \n \n int main() {\n \n \n int a, b;\n \n \n \n \n \n // Simple Assignment Operator\n \n \n a = 13;\n \n \n b = 9;\n \n \n \n \n \n cout << "Value of a: " << a << endl;\n \n \n cout << "Value of b: " << b << endl;\n \n \n \n \n \n //Addition Assignment Operator\n \n \n a += b;\n \n \n cout << "New Value of a: " << a << endl;\n \n \n \n \n \n //Subtraction Assignment Operator\n \n \n a -= b;\n \n \n cout << "New Value of a: " << a << endl;\n \n \n \n \n \n //Multiplication Assignment Operator\n \n \n a *= b;\n \n \n cout << "New Value of a: " << a << endl;\n \n \n \n \n \n //Division Assignment Operator\n \n \n a /= b;\n \n \n cout << "New Value of a: " << a << endl;\n \n \n \n \n \n //Modulus Assignment Operator\n \n \n a %= b;\n \n \n cout << "New Value of a: " << a << endl;\n \n \n \n \n \n return 0;\n \n \n }\n \n \n \n \n \n </iostream>
Output:
Value of a: 13Value of b: 9New Value of a: 22New Value of a: 13New Value of a: 117New Value of a: 13New Value of a: 4
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 \n <iostream>\n \n \n \n \n \n using namespace std;\n \n \n \n \n \n int main() {\n \n \n int a, b;\n \n \n a = 6;\n \n \n b = 12;\n \n \n bool result;\n \n \n \n \n \n result = (a == b); // false\n \n \n cout << "6 == 12 is " << result << endl;\n \n \n \n \n \n result = (a != b); // true\n \n \n cout << "6 != 12 is " << result << endl;\n \n \n \n \n \n result = a > b; // false\n \n \n cout << "6 > 12 is " << result << endl;\n \n \n \n \n \n result = a < b; // true\n \n \n cout << "6 < 12 is " << result << endl;\n \n \n \n \n \n result = a >= b; // false\n \n \n cout << "6 >= 12 is " << result << endl;\n \n \n \n \n \n result = a <= b; // true\n \n \n cout << "6 <= 12 is " << result << endl;\n \n \n \n \n \n return 0;\n \n \n }\n \n \n \n \n \n </iostream>
Output:
6 == 12 is 06 != 12 is 16 > 12 is 06 < 12 is 16 >= 12 is 06 <= 12 is 1
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 = 6b = 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
Now letโs look at how these operators are implemented in C++ through the following example:
#include \n \n \n <iostream>\n \n \n \n \n \n using namespace std;\n \n \n \n \n \n int main() {\n \n \n bool result;\n \n \n \n \n \n result = (4 != 6) && (4 < 6); // true\n \n \n cout << "(4 != 6) && (4 < 6) is " << result << endl;\n \n \n \n \n \n result = (4 == 6) && (4 < 6); // false\n \n \n cout << "(4 == 6) && (4 < 6) is " << result << endl;\n \n \n \n \n \n result = (4 == 6) && (4 > 6); // false\n \n \n cout << "(4 == 6) && (4 > 6) is " << result << endl;\n \n \n \n \n \n result = (4 != 6) || (4 < 6); // true\n \n \n cout << "(4 != 6) || (4 < 6) is " << result << endl;\n \n \n \n \n \n result = (4 != 6) || (4 > 6); // true\n \n \n cout << "(4 != 6) || (4 > 6) is " << result << endl;\n \n \n \n \n \n result = (4 == 6) || (4 > 6); // false\n \n \n cout << "(4 == 6) || (4 > 6) is " << result << endl;\n \n \n \n \n \n result = !(6 == 3); // true\n \n \n cout << "!(6 == 3) is " << result << endl;\n \n \n \n \n \n result = !(6 == 6); // false\n \n \n cout << "!(6 == 6) is " << result << endl;\n \n \n \n \n \n return 0;\n \n \n }\n \n \n \n \n \n </iostream>
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
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 \n <iostream>\n \n \n \n \n \n using namespace std;\n \n \n \n \n \n int main() {\n \n \n unsigned int a = 60; // 60 = 0011 1100 \n \n \n unsigned int b = 13; // 13 = 0000 1101\n \n \n int c = 0;\n \n \n \n \n \n c = a & b;\n \n \n cout << "a & b: " << c << endl; // 12 = 0000 1100\n \n \n \n \n \n c = a | b;\n \n \n cout << "a | b: " << c << endl; // 61 = 0011 1101\n \n \n \n \n \n c = a ^ b;\n \n \n cout << "a ^ b: " << c << endl; // 49 = 0011 0001\n \n \n \n \n \n c = ~a;\n \n \n cout << "~a: " << c << endl; // -61 = 1100 0011\n \n \n \n \n \n c = a << 2;\n \n \n cout << "a << 2: " << c << endl; // 240 = 1111 0000\n \n \n \n \n \n c = a >> 2;\n \n \n cout << "a >> 2: " << c << endl; // 15 = 0000 1111\n \n \n \n \n \n return 0;\n \n \n }\n \n \n \n \n \n </iostream>
Output:
a & b: 12a | b: 61a ^ b: 49~a: -61a<<2: 240a>>2: 15
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 \n <iostream>\n \n \n \n \n \n using namespace std;\n \n \n int main() {\n \n \n cout<<"Size of int : "<< sizeof(int) << "\n";\n \n \n \n \n \n cout<<"Size of char : " << sizeof(char) << "\n";\n \n \n \n \n \n cout<<"Size of float : " << sizeof(float) << "\n";\n \n \n \n \n \n cout<<"Size of double : " << sizeof(double) << "\n";\n \n \n \n \n \n return 0;\n \n \n }\n \n \n \n \n \n </iostream>
Output:
Size of int: 4Size of char: 1Size of float: 4Size of double: 8
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 \n <iostream>\n \n \n \n \n \n using namespace std;\n \n \n int main() {\n \n \n int a = 1;\n \n \n int b;\n \n \n \n \n \n //conditional operator\n \n \n b = (a < 10) ? 2 : 5;\n \n \n \n \n \n //display result\n \n \n cout << "The value of b is: " << b << endl;\n \n \n return 0;\n \n \n }\n \n \n \n \n \n </iostream>
Output:
The value of b is: 2
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 \n <iostream>\n \n \n \n \n \n using namespace std;\n \n \n int main() {\n \n \n int a, b;\n \n \n b = 100;\n \n \n \n \n \n //comma operator\n \n \n a = (b++, b + 10, 99 + b);\n \n \n \n \n \n //display output\n \n \n cout << a;\n \n \n return 0;\n \n \n }\n \n \n \n \n \n </iostream>
Output:
200
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
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