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.

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 –

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: 20Difference: 14Product: 51Quotient: 5Remainder: 2Incremented a: 18Decremented b: 2Copy 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 –

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: 13Value of b: 9New Value of a: 22New Value of a: 13New Value of a: 117New Value of a: 13New Value of a: 4Copy 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 –

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 06 != 12 is 16 > 12 is 06 < 12 is 16 >= 12 is 06 <= 12 is 1Copy 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 –

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 falseCopy 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 0Copy code`
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
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++
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 –

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: 12a | b: 61a ^ b: 49~a: -61a<<2: 240a>>2: 15Copy 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: 4Size of char: 1Size of float: 4Size of double: 8Copy 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: 2Copy 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:

` `
`200Copy 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++:

### 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