Operators in C++
Operators in C++ are the symbols used for performing operations on the values or the variables. Operator tells the compiler to perform a mathematical or logical operation. There are different types of operators for performing different operations.
An operator operates operands. For example:
int c = a + b;
Here, ‘+’ is the addition operator, ‘a’ and ‘b’ are the operands.
Types of Operators in C++
There are six different types of operators in C++:
- Arithmetic Operators
- Assignment Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Other Operators
Arithmetic Operators
Arithmetic operators are used to perform arithmetic or mathematical operations such as addition, subtraction, multiplication or division, etc.
There are two types of Arithmetic operators:
- Unary Operators — Unary arithmetic operators only work with a single operand. There are four types of unary operators:
- Unary plus - The symbol + is used to represent the unary plus operator. It does not change the operand value.
- Unary minus - The symbol - is used to represent the unary plus operator. It does not change the operand value.
- Increment Operator - The symbol ++ is used to represent the increment operator. It can be used as a pre-increment or a post-increment operator. The pre-increment operator uses symbol ++i where value of operand i is increased by 1 before it is used. The post-increment operator uses symbol i++ where the value of operand i is increased by 1 after it is used.
- Decrement Operator - The symbol -- is used to represent the increment operator. It can be used as a pre-decrement or a post-decrement operator. The pre-decrement operator uses the symbol --i where value of operand i is decreased by 1 before it is used. The post-decrement operator uses symbol i-- where the value of operand i is decreased by 1 after it is used.
- Binary Operators — Binary Operators are used with two operands to perform the arithmetic operations. Binary arithmetic operators are shown in the following table with examples:
a = +1
b = a //here b = 1
a = -1
b = a //here b = -1
The following program demonstrates the use of the increment operator:
#include <iostream>
using namespace std;
int main()
{
int i = 10;
cout << "with post-increment i is " << i++ << "\n";
cout << "After post-increment i is " << i << "\n";
int j = 20;
cout << "Before pre-increment j is " << j << "\n";
cout << "with pre-increment j is " << ++j << "\n";
return 0;
}
Output
After post-increment i is 11
Before pre-increment j is 20
with pre-increment j is 21
From the above example, we can understand that with ++j, the value of the variable i is incremented first and then it is used. In i++, the value of the variable is used first and then it is incremented. The decrement operator behaves in a similar way.
The following program shows the use of the decrement operator:
#include <iostream>
using namespace std;
int main()
{
int i = 10;
cout << "with post-decrement i is " << i-- << "\n";
cout << "After post-decrement i is " << i << "\n";
int j = 20;
cout << "Before pre-decrement j is " << j << "\n";
cout << "with pre-decrement j is " << ++j << "\n";
return 0;
}
Output
After post-decrement i is 9
Before pre-decrement j is 20
with pre-decrement j is 21
Symbol | Name | Description | Example |
---|---|---|---|
+ | Addition | Adds two operands. | int a = 3, b = 6; int c = a + b; // c = 9 |
- | Substraction | Substracts second operand from the first. | int a = 9, b = 6; int c = a - b; // c = 3 |
* | Multiplication | Multiplies two operands. | int a = 3, b = 6; int c = a * b; // c = 18 |
/ | Division | Divides first operand by the second operand. | int a = 12, b = 6; int c = a / b; // c = 2 |
% | Modulo | Returns the remainder after the division of the first operand by the second operand. | int a = 8, b = 6; int c = a % b; // c = 2 |
The following program demonstrates the use of Binary Arithmetic operators in C++:
#include <iostream>
using namespace std;
int main()
{
int a = 8, b = 3;
// Addition operator
cout << "a + b = " << (a + b) << endl;
// Subtraction operator
cout << "a - b = " << (a - b) << endl;
// Multiplication operator
cout << "a * b = " << (a * b) << endl;
// Division operator
cout << "a / b = " << (a / b) << endl;
// Modulo operator
cout << "a % b = " << (a % b) << endl;
return 0;
}
Output
a - b = 5
a * b = 24
a / b = 2
a % b = 2
Assignment Operators
In C++, Assignment Operators are used to assign a value to a variable.
Assignment operators are shown in the following table with examples:
Symbol | Name | Description | Example |
---|---|---|---|
= | Assignment Operator | Assigns the value on the right to the variable on the left. | int a = 2; // a = 2 |
+= | Add and Assignment Operator | With the use of this operator, the value of the left variable is first added to the value of the right variable and then assigns the result to the left variable. | int a = 8, b = 3; a += b; // a = 11 |
-= | Substract and Assignment Operator | With the use of this operator, the value of the left variable is first substracted with the value of the right variable and then assigns the result to the left variable. | int a = 8, b = 3; a -= b; // a = 5 |
*= | Multiply and Assignment Operator | With the use of this operator, the value of the left variable is first multiplied with the value of the right variable and then assigns the result to the left variable. | int a = 8, b = 3; a *= b; // a = 24 |
/= | Divide and Assignment Operator | With the use of this operator, the value of the left variable is first divided by the value of the right variable and then assigns the result to the left variable. | int a = 8, b = 3; a /= b; // a = 2 |
The following program demonstrates the use of Assignment Operators:
#include <iostream>
using namespace std;
int main()
{
int a = 6, b = 4;
// Assignment Operator
cout << "a = " << a << endl;
// Add and Assignment Operator
cout << "a += b is " << (a += b) << endl;
// Subtract and Assignment Operator
cout << "a -= b is " << (a -= b) << endl;
// Multiply and Assignment Operator
cout << "a *= b is " << (a *= b) << endl;
// Divide and Assignment Operator
cout << "a /= b is " << (a /= b) << endl;
return 0;
}
Output
a += b is 10
a -= b is 6
a *= b is 24
a /= b is 6
Relational Operators
Relational Operators are used when comparing the values of two operands. It is used when you need to know which operand is greater or which operand is less than the other. The result of using relation operator is always a Boolean value true or false.
The following table shows Relational operators with example in C++:
Symbol | Name | Description | Example |
---|---|---|---|
== | Is Equal To | This operator checks if the value of the left variable is equal to the value of the right variable and returns true or false. | int a = 10, b = 5; bool x = (a == b) //x is false |
> | Greater Than | This operator checks if the value of the left variable is greater than the value of the right variable. | int a = 10, b = 5; bool x = (a > b) //x is true |
>= | Greater Than or Equal To | This operator checks if the value of the left variable is greater than or equal to the value of the right variable. | int a = 10, b = 5; bool x = (a >= b) //x is true |
< | Less Than | This operator checks if the value of the left variable is less than the value of the right variable. | int a = 10, b = 5; bool x = (a < b) //x is false |
<= | Less Than or Equal To | This operator checks if the value of the left variable is less than or equal to the value of the right variable. | int a = 10, b = 5; bool x = (a <= b) //x is false |
!= | Not Equal To | This operator checks if the value of the left variable is not equal to the value of the right variable. | int a = 10, b = 5; bool x = (a != b) //x is true |
The following program demonstrates the use of Relational operators:
#include <iostream>
using namespace std;
int main()
{
int a = 7, b = 3;
// Equal to operator
cout << "a == b is " << (a == b) << endl;
// Greater than operator
cout << "a > b is " << (a > b) << endl;
// Greater than or Equal to operator
cout << "a >= b is " << (a >= b) << endl;
// Lesser than operator
cout << "a < b is " << (a < b) << endl;
// Lesser than or Equal to operator
cout << "a <= b is " << (a <= b) << endl;
// true
cout << "a != b is " << (a != b) << endl;
return 0;
}
Output
a > b is 1 // 1 is true
a >= b is 1
a < b is 0
a <= b is 0
a != b is 1
Logical Operators
Logical operators are used to check whether a condition is true or false. If the condition is true, it returns 1 whereas if the condition is false, it returns 0.
Logical operators are shown in the following table with examples:
Symbol | Name | Description | Example |
---|---|---|---|
&& | Logical AND | This operator is used to check two or more conditions. It returns true or 1 if all the conditions check are true. | int a = 11, b = 15; bool x = (a < b && a == 11); //x is true or 1 |
|| | Logical OR | This operator returns true if at least one of the conditions is true. | int a = 10, b = 5; bool x = (a == b || a > b); //x is true |
! | Logical NOT | This operator returns true if the condition is false. | bool a = false; bool x = (!a); //x is true or 1 |
The following program demonstrates the use of Logical Operators:
#include <iostream>
using namespace std;
int main()
{
int a = 6, b = 4;
// Logical AND operator
cout << "a && b is " << (a && b) << endl;
// Logical OR operator
cout << "a ! b is " << (a > b) << endl;
// Logical NOT operator
cout << "!b is " << (!b) << endl;
return 0;
}
Output
a ! b is 1
!b is 0
Bitwise Operators
Bitwise Operators are used to perform bit-level operations on the operands. First, operators are converted to bit level then the calculation is performed on the operands. The arithmetic operations like addition, subtraction, multiplication, etc. can be done at the bit-level for faster processing.
The following table shows Relational operators with example in C++:
Symbol | Name | Description | Example |
---|---|---|---|
& | Bitwise AND | It takes two operand values then performs AND on each bit of operand values. If both are 1, AND returns 1, otherwise 0, which means (1, 1) returns 1 and ((1,0),(0,0),(0,1)) returns 0. | int a = 2, b = 3; (a & b);//returns 2 |
| | Bitwise OR | This operator returns 1 if at least one of the bits is 1 else 0 if both bits are 0. | int a = 2, b = 3; (a | b);//returns 3 |
^ | Bitwise XOR | This operator returns 1 if both bits are different. | int a = 2, b = 3; (a ^ b);//returns 1 |
<< | Left Shift | This operator shifts all the bits towards the left and adds trailing 0s to the right. | int a = 2, b = 3; (a << 1);//returns 4 |
>> | Right Shift | This operator shifts all the bits towards the right and the rightmost bits are then discarded. | int a = 2, b = 3; (a >> 1);//returns 1 |
~ | One's Complement | This operator changes binary digits 1 to 0 and 0 to 1. | int b = 3; (~b);//returns -4 |
The following program demonstrates the use of Bitwise Operators in C++:
#include <iostream>
using namespace std;
int main()
{
// bitwise NOT
cout << "~" << "10" << " is " << ~10 << endl;
// bitwise AND
cout << "4" << " & " << "5" << " is " << (4 & 5) << endl;
// bitwise OR
cout << "4" << " | " << "5" << " is " << (4 | 5) << endl;
// bitwise XOR
cout << "4" << " ^ " << "5" << " is " << (4 ^ 5) << endl;
// bitwise left shift
cout << "4" << " << " << "2" << " is " << (4 << 2) << endl;
// bitwise right shift
cout << "4" << " >> " << "2" << " is " << (4 >> 2) << endl;
return 0;
}
Output
4 & 5 is 4
4 | 5 is 5
4 ^ 5 is 1
4 << 2 is 16
4 >> 2 is 1
Other Operators
Here is a list of some other common operators available in C++:
Operator | Description | Example |
---|---|---|
sizeof | It returns the size of the data type. | sizeof(int); //4 |
?: | It returns value based on the condition. | string result = (5 > 0) ? "even" : "odd"; //"even" |
& | It represents the memory address of the operand. | # //address of num |
. | It accesses members of class objects. | s1.marks = 97; |
-> | It is used with pointers to access the class or variables. | ptr->marks = 97; |
<< | It prints the output value. | cout << 5; |
>> | It gets the input value. | cin >> num; |