In C++, operators are special symbols that are used to perform operations on variables and values on operands. Operators are essential concepts of any programming language. There can be several operations, including arithmetic , value assignment, bitwise, and logical computations.
C++ Operators Example
Let us take a basic example to illustrate 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 discuss these C++ operators one by one with their types and examples.
1. Arithmetic Operators:
In C++, arithmetic operations are mainly utilized to perform mathematical operations (Like (+) addition , (-) subtraction, (*) multiplication, (/) division) on the operands. We can categorize these arithmetic operations into two categories, including binary and unary operators. Here is the following table that helps to understand the working 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 us take a C++ program to calculate the Arithmetic 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 mainly utilized to compare the values of two operands. It provides the result in Boolean values (0 and 1). If the comparison is true, it returns 1 value. On the other hand, if the comparison is false, it returns 0 value. There are several operators in relational operators. Here is the following table that helps to understand the working 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 | < | This operator is used to check the first operand value is lesser than the second operand value. If the condition matches, it returns true; otherwise, false. |
| Less Than or Equal To | <= | It checks that the first operand value is lesser than or equal to the second operand value. If the condition matches, it returns true; otherwise, false. |
| Not Equal To | != | It checks both operand values are not equal. If the condition matches, it returns true; otherwise, false. |
Relational Operators Example:
Let us take a C++ program to calculate the relational operators.
Example
#include <iostream>
using namespace std;
int main() {
int a = 16;
int b = 7;
cout << (a == b) << endl; // 16 == 7 (Condition False, returns 0)
cout << (a > b) << endl; // 16 > 7 (Condition True, returns 1)
cout << (a < b) << endl; // 16 < 7 (Condition False, returns 0)
cout << (a >= b) << endl; // (16 >= 7) (Condition True, returns 1)
cout << (a <= b) << endl; //16 <= 7 (Condition False, returns 0)
cout << (a != b) << endl; //(16 != 7) (Condition True, returns 1)
return 0;
}
Output:
0
1
0
1
0
1
3. Logical Operators
In C++, logical operators are mainly utilized to perform logical operations on Boolean values. These operators are also utilized for combining two or multiple conditions. It provides the result in Boolean values (0 and 1). If the comparison is true, it returns 1 value. If the comparison is false, it returns 0 value. Here is the following table that helps to understand the working of these operators in C++:
| Operator Name | Operator Symbol | Description | ||
|---|---|---|---|---|
| Logical AND | && | If all operand's values are true or non-zero, it returns only the true value. | ||
| Logical OR | If either of the operand's values is true or non-zero, it returns true value. | |||
| Logical NOT | ! | If the operand value is false or zero, it returns a true value. |
Logical Operators Example:
Let us take a C++ program to illustrate the Logical Operators.
Example
#include <bits/stdc++.h>
using namespace std;
int main() {
int x = 18, y = 12;
cout << "The value of x && y is " << (x && y) << endl; // Logical AND
cout << "The value of x || y is " << (x || y) << endl; // Logical OR
cout << "The value of !y is " << (!y); // Logical NOT
return 0;
}
Output:
The value of x && y is 1
The value of x || y is 1
The value of !y is 0
4. Bitwise Operators
Bitwise operators are mainly utilized to perform operations on the bit level. It works on individual bits. The operand is first converted into the bit level, and then the operation is performed on them. It performs bit by bit operation. Here is the following table that helps to understand the working of these operators in C++:
| Operator Name | Operator Symbol | Description | |
|---|---|---|---|
| Binary AND | & | If this operator is available in both operands, it copies a bit to the calculated result. | |
| Bitwise OR | If this operator is available in any of the operands, it copies a bit to the calculated result. | ||
| Bitwise XOR | ^ | If this operator is present in any of the operands but not both, it copies a bit to the calculated result. | |
| Binary One's Complement | ~ | This operator changes the binary values 1 to 0 and 0 to 1. | |
| Right Shift | >> | This operator shifts the value to the right by a number of bits specified by the right operands. | |
| Left Shift | << | This operator shifts the value to the left by a number of bits specified by the right operands. |
Bitwise Operators Example:
Let us take a C++ program to illustrate the Bitwise Operators.
Example
#include <bits/stdc++.h>
using namespace std;
int main() {
int x = 14, y = 8;
cout << "x & y is " << (x & y) << endl; // Binary AND operator
cout << "x | y is " << (x | y) << endl; // Binary OR operator
cout << "x ^ y is " << (x ^ y) << endl; // Binary XOR operator
cout << "~(x) is " << ~(x) << endl; // One's Complement operator
cout << "x<<1 is " << (x << 1) << endl; // Left Shift operator
cout << "x>>1 is " << (x >> 1); // Right Shift operator
return 0;
}
Output:
x & y is 8
x | y is 14
x ^ y is 6
~(x) is -15
x<<1 is 28
x>>1 is 7
5. Assignment Operators
These assignment operators are mainly utilized to assign the values to a variable. The most commonly utilized operator is the assignment operator (=). These operators enable us to change and update the value stored in the variable. Here is the following table that helps to understand the working of these operators in C++:
| Operator Name | Operator Symbol | Description |
|---|---|---|
| Assignment | = | It assigns the values from the right side operands to left side operand. |
| Add and Assignment | += | This operator first adds the right operand value to the left operand, and then the value is assign to the left operand. |
| Subtract and Assignment | -= | This operator first subtracts the right operand value from the left operand, and then the value is assign to the left operand. |
| Multiply and Assignment | *= | This operator first multiply the right operand with the left operand, and then the value is assign to the left operand. |
| Divide and Assignment | /= | This operator divides the left operand with the right operand, and then the value is assigns to the left operand. |
| Modulus and Assignment | %= | It takes remainders using two operands, and then the value is assign to the left operand. |
Assignment Operators Example:
Let us take a C++ program to illustrate the Assignment Operators.
Example
#include <bits/stdc++.h>
using namespace std;
int main() {
int x = 18, y = 5;
cout << "The value of x = " << x << endl; // Assignment Operator.
cout << "The value of x += y is " << (x += y) << endl; // Add and Assignment Operator.
cout << "The value of x -= y is " << (x -= y) << endl; // Subtract and Assignment Operator.
cout << "The value of x *= y is " << (x *= y) << endl; // Multiply and Assignment Operator.
cout << "The value of x /= y is " << (x /= y) << endl; // Divide and Assignment Operator.
cout << "The value of x %= y is " << (x %= y); // Modulus and Assignment Operator.
return 0;
}
Output:
The value of x = 18
The value of x += y is 23
The value of x -= y is 18
The value of x *= y is 90
The value of x /= y is 18
The value of x %= y is 3
6. Ternary or Conditional Operator
The ternary or conditional operator performs operations on three operands. It is a conditional-based operator. It is mainly used to run a set of statements when the test expression is true, and another set of statements is executed when the test expression evaluates to false.
Syntax:
It has the following syntax:
(Condition a) ? Expression1 : Expression2;
Here, two conditions are occurred:
- If condition a is true, Expression1 is executed and gives the appropriate result.
- If condition a is false, Expression2 is executed and gives the appropriate result.
Ternary or Conditional Operator Example:
Let us take a C++ program to illustrate the Ternary and Conditional Operators.
Example
#include <iostream>
#include <string>
using namespace std;
int main() {
int Score;
cout << "Enter the Player Score: ";
cin >> Score;
// ternary operator checks
string result = (Score >= 75) ? "Won" : "Lose";
cout << "Player " << result << " the Game.";
return 0;
}
Output:
Enter the Player Score: 80
Player Won the Game.
7. Misc Operators
Misc operators is an abbreviation for miscellaneous operators. There are several misc operators that don't fit in any of the above categories, such as arithmetic, logical, bitwise, and many operators. Several miscellaneous operators in C++ are as follows:
- Sizeof Operators:
The Sizeof operator is utilized to return the size of the operand, variables, and data types in bytes.
Syntax:
It has the following syntax:
Sizeof(char);
Sizof (var_name);
Sizeof (int);
Example:
Let us take an example to illustrate the sizeof operator in C++.
Example
#include <iostream>
using namespace std;
int main()
{
int a = 10;
cout <<sizeof (a) << endl; //Sizeof(int) operator
cout <<sizeof (char) << endl; //Sizeof(int) operator
return 0;
}
Output:
- Comma Operator (,):
Comma operators are mainly utilized to separate the operands, variables , data types , etc. It is used to perform several operations. It evaluates the multiple expression and returns the last expression of the comma-separated list.
Syntax:
It has the following syntax:
int a, b, c;
Example:
Let us take an example to illustrate the comma operator in C++.
Example
#include <iostream>
using namespace std;
int main()
{
int x = 34, y = 45;
cout << x << " " << y << " " << endl; //Comma operator
return 0;
}
Output:
34, 45
- Arrow and dot Operator:
Dot operators are mainly utilized to access members of an object. On the other hand, array operators are utilized for accessing the variables of the classes or structures via its pointer.
Syntax:
It has the following syntax:
Obj.print();
Ptr -> member;
Example:
Let us take an example to illustrate the arrow and dot operator in C++.
Example
#include<iostream>
using namespace std;
class CppTutorial {
public:
int x = 10;
};
int main()
{
CppTutorial obj;
CppTutorial *ptr = &obj;
cout << obj.x << endl; // Using . (dot) operator
cout << ptr->x << endl; // Using -> (arrow) operator
return 0;
}
Output:
10
10
- Casting Operator:
Using the casting operator, we can convert one data type to another data type.
Syntax:
int var = static_cast<int>(var);
Example:
Let us take an example to illustrate the casting operator in C++.
Example
#include <iostream>
using namespace std;
int main() {
double x = 4578.05575;
float y = 19.845;
int a = static_cast<int>(x); //Casting operator
int b = static_cast<int>(y);
cout << "The Value of (int)b is:" << a <<endl; //shows the converted value
cout << "The Value of (int)b is:" << b;
return 0;
}
Output:
The Value of (int)b is:4578
The Value of (int)b is:19
- Addressof Operator (&) and Pointer Operator (*)
It is mainly utilized to return the memory address of a variable. On the other hand, pointer operator is used to define a pointer to a variable.
Syntax:
&var_name;
*var;
Example:
Let us take an example to illustrate the addressof(&) operator and pointer (*) operator in C++.
Example
#include <iostream>
using namespace std;
int main()
{
int num = 46;
int var;
int* ptr = # //address and pointer variable
cout << "The address of the variable num is: " << ptr <<endl;
cout << "The address of the variable var is: " << var;
return 0;
}
Output:
The address of the variable num is: 0x7ffe95d31498
The address of the variable var is: 30841
Operator Precedency and Associativity
Operator precedence and associativity are essential concepts in C++ programming. It defines the order, and part of the expression is calculated. Precedency shows that portion of the expression should be evaluated first. On the other hand, associativity shows which direction to calculate when precedency operators are in expression. Here, we will discuss both operators:
Precedence of Operators in C++
The precedence of operator species that which operator will be evaluated first and next. The associativity specifies the operator's direction to be evaluated, and it may be left to right or right to left.
Let us understand the precedence by the example given below:
int data= 12*12 + 4;
will be calculated as:
(12 * 12) + 4 = 148
The "data" variable will contain 148 because * (multiplicative operator) is evaluated before + (additive operator).
Operator Associativity:
In operator associativity, if the expression has multiple operators with similar precedence, the evaluation starts from left to right or right to left.
For example:
int data= 36 / 12 * 4
will be evaluated as:
(36 / 12) * 4 = 12
The precedence and associativity of C++ operators is given below:
| S.No | Category | Operator | Associativity | ||
|---|---|---|---|---|---|
1. |
Scope Resolution | :: | Left to Right | ||
2. |
Postfix | () [] -> . ++ - - | Left to right | ||
3. |
Unary | + - ! ~ ++ - - (type)* & sizeof | Right to left | ||
4. |
Multiplicative | * / % | Left to right | ||
5. |
Additive | + - | Right to left | ||
6. |
Shift | << >> | Left to right | ||
7. |
Relational | < <= > >= | 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
- What is the output of the 7 << 4 Expression in C++?
- What will be the output of the following code:
#include <iostream>
using namespace std;
int main() {
int x;
x = 12 - 2 + 3 * 7;
cout << "x: " << x;
return 0;
}
- What will be the output of the following code?
#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