In C++, operators are unique symbols utilized to carry out actions on variables and values as operands. Operators play a crucial role in all programming languages, enabling a wide range of operations such as arithmetic calculations, assigning values, bitwise manipulations, and logical evaluations.
C++ Operators Example
Let's consider a simple example to demonstrate operators in C++.
Example
#include <iostream>
using namespace std;
int main()
{
int a = 20;
int b = 30;
int c = a + b;
cout << "c: " << c<< endl;
return 0;
}
Output:
Types of Operators
There are following types of operators to perform different types of operations in C++ language.
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Ternary or Conditional Operators
- Misc Operators
Here, we will explore each of these C++ operators individually, detailing their categories and providing illustrative examples.
1. Arithmetic Operators:
In C++, arithmetic calculations are primarily used to carry out mathematical computations (such as (+) addition, (-) subtraction, (*) multiplication, (/) division) on the given values. These arithmetic computations in C++ are divided into two groups: binary and unary operators. Below is a table illustrating the functionality of these operators in C++:
| Operators | Name | Symbol | Description |
|---|---|---|---|
| Binary | Addition | + | It is utilized to add two values. |
|
Subtraction | - | It is utilized to subtract one value from another value. |
|
Multiplication | * | It is utilized to multiply two values. |
|
Division | / | It is utilized to divide one value from another. |
|
Modulus | % | It is utilized to find the remainder value after the division. |
| Unary | Increment | ++ | It is utilized to increase the value by 1. |
|
Decrement | -- | It is utilized to decrease the value by 1. |
Arithmetic Operators Example:
Let's consider a C++ program that performs arithmetic calculations using operators.
Example
#include <iostream>
using namespace std;
int main()
{
int x = 15, y = 4;
int add = x + y;
int sub = x - y;
int multipli = x * y;
int division = x / y;
int modulus = x % y;
int increment = ++x;
int decrement = --x;
cout << "Addition: " << add << endl; // 15 + 4 = 19
cout << "Subtraction: " << sub << endl; // 15 - 4 = 11
cout << "Multiplication: " << multipli << endl; // 15 * 4 = 60
cout << "Division: " << division << endl; // 15 / 4 = 3 (Quotient)
cout << "Modulus: " << modulus << endl; // 15 / 4 = 3 (Here, 3 is Remainder)
cout << "Increment: " << increment <<endl; // 1+ 15 = 16 (Here, the value of x is 16)
cout << "Decrement: " << decrement <<endl; // 16 - 1 = 15
}
Output:
Addition: 19
Subtraction: 11
Multiplication: 60
Division: 3
Modulus: 3
Increment: 16
Decrement: 15
2. Relational Operators:
In C++, relational operators are primarily used to evaluate the values of two operands. They yield outcomes in Boolean values (0 and 1). In case the evaluation is correct, it yields a value of 1. Conversely, if the evaluation is incorrect, it yields a value of 0. Various operators fall under relational operators. The table below illustrates the functionality of these operators in C++:
| Operator Name | Operator Symbol | Description | |||
|---|---|---|---|---|---|
| Is Equal To operator | == | It checks both operands are equal or not. If the condition matches, it returns true; otherwise, false. | |||
| Greater Than | > | This operator is used to check the first operand value is greater than the second operand value. If the condition matches, it returns true; otherwise, false. | |||
| Greater Than or Equal To | >= | This operator is used to check the first operand value is greater than or equal to the second operand value. If the condition matches, it returns true; otherwise, false. | |||
| Less Than | _PRESERVE32__> | This operator shifts the value to the right by a number of bits specified by the right operands. | |||
| Left Shift | _PRESERVE33__ . ++ - - | Left to right | |||
3. |
Unary | + - ! ~ ++ - - (type)* & sizeof | Right to left | ||
4. |
Multiplicative | * / % | Left to right | ||
5. |
Additive | + - | Right to left | ||
6. |
Shift | _PRESERVE34__> | Left to right | ||
7. |
Relational | _PRESERVE35__ >= | Left to right | ||
8. |
Equality | == !=/td> | Right to left | ||
9. |
Bitwise AND | & | Left to right | ||
10. |
Bitwise XOR | ^ | Left to right | ||
11. |
Bitwise OR | Right to left | |||
12. |
Logical AND | && | Left to right | ||
13. |
Logical OR | Left to right | |||
14. |
Conditional | ?: | Right to left | ||
15. |
Assignment | = += -= *= /= %=>>= <<= &= ^= | = | Right to left | |
16. |
Comma | , | Left to right |
C++ Operators MCQs:
- What is the main role of the assignment operator (=) in C++?
- To perform addition operation
- To compare two values
- To assign a value to a variable
- To perform Decrement operation
(c) To assign a value to a variable
What is the result of the 7 << 4 Expression in C++?
- What is the result of the code snippet below:
#include <iostream>
using namespace std;
int main() {
int x;
x = 12 - 2 + 3 * 7;
cout << "x: " << x;
return 0;
}
(a) 31
- What is the expected result of executing the code snippet provided below?
#include <iostream>
using namespace std;
int main() {
int x = 18, y = 25, z;
z = (x < y) ? x : y;
cout << z;
return 0;
}
- What is the main purpose of the comma operator (,) in C++?
- Used to compare two values
- Used to run two or more expressions in sequence
- Used to separate variables in declarations
- Used to separate functions arguments
Rewritten: Utilized for executing multiple expressions sequentially