C++ Operators
In this tutorial, we will learn about the different types of operators in C++ with the help of examples. In programming, an operator is a symbol that operates on a value or a variable.
Operators
Operators are symbols that perform operations on variables and Constants. An operator is a symbol that helps to perform specific mathematical and logical computations on operands.
For example, +
is an operator used for addition, while -
is an operator used for subtraction.
Operators in C++ can be classified into 6 types:
1) Arithmetic Operators
2) Assignment Operators
3) Relational Operators
4) Logical Operators
5) Bitwise Operators
6) Other Operators
1. C++ Arithmetic Operators
Arithmetic operators are used to perform arithmetic/mathematical operations on variables and data.
Arithmetic operator are of two types:
1) Unary Operators :- Operators that operates or works with a single operand are unary operators.
For example:- (++), (--) etc.
2) Binary Operators:- Operators that operates or works with two operands are binary operators.
For example :- +, -, *, / etc.
For example:-
a + b;
Here, the +
operator is used to add two variables a and b. Similarly there are various other arithmetic operators in C++.
List of Binary Operators:-
Operator | Operation |
---|---|
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
/ |
Division |
% |
Modulo Operation (Remainder after division) |
Example 1: Program to understand Arithmetic Operators
// Program to understand Arithmetic Operators
#include <iostream>
using namespace std;
int main() {
int a, b;
a = 5;
b = 2;
// printing the sum of a and b
cout << "a + b = " << (a + b) << endl;
// printing the difference of a and b
cout << "a - b = " << (a - b) << endl;
// printing the product of a and b
cout << "a * b = " << (a * b) << endl;
// printing the division of a by b
cout << "a / b = " << (a / b) << endl;
// printing the modulo of a by b
cout << "a % b = " << (a % b) << endl;
return 0;
}
Output
a + b = 7 a - b = 3 a * b = 10 a / b = 2 a % b = 1
Here, the operators +
, -
and *
compute addition, subtraction, and multiplication respectively as we might have expected.
Division Operator /
The operation (a / b)
in our program. The /
operator is the division operator.
As we can see from the above example, if an integer is divided by another integer, we will get the quotient. However, if either divisor or dividend is a floating-point number, we will get the result in decimals.
In C++, 5/2 is 2 5.0 / 2 is 2.5 5 / 2.0 is 2.5 5.0 / 2.0 is 2.5
Modulo Operator %
The modulo operator %
computes the remainder. When a = 5
is divided by b = 2
, the remainder is 1.
Note: The %
operator can only be used with integers.
Increment and Decrement Operators
C++ also provides increment and decrement operators: ++
and --
respectively. ++
increases the value of the operand by 1, while --
decreases it by 1.
For example:-
int num = 2;
// increasing num by 1
++num;
Here, the value of num gets increased to 3 from its initial value of 2.
Example 2: Program to understand Increment and Decrement Operators
// Program to understand increment and decrement operators
#include <iostream>
using namespace std;
int main() {
int a = 1, b = 10, result_a, result_b;
// incrementing a by 1 and storing the result in result_a
result_a = ++a;
cout << "result_a = " << result_a << endl;
// decrementing b by 1 and storing the result in result_b
result_b = --b;
cout << "result_b = " << result_b << endl;
return 0;
}
Output
result_a = 2 result_b = 9
In the above program, we used ++
and --
operator as prefixes. We can also use these operators as postfix.
There is a slight difference when these operators are used as a prefix.
To learn more about these operators, visit increment and decrement operators.
2. C++ Assignment Operators
In C++, assignment operators are used to assign values to variables. = symbol is used for assignment operator.
Assignment operator is the only operator which can be overloaded but cannot be inherited
For example:-
// assign 2 to a
a = 2;
Here, we have assigned a value of 2
to the variable a.
List of Assignment Operators:-
Operator | Example | Equivalent to |
---|---|---|
= |
a = b; |
a = b; |
+= |
a += b; |
a = a + b; |
-= |
a -= b; |
a = a - b; |
*= |
a *= b; |
a = a * b; |
/= |
a /= b; |
a = a / b; |
%= |
a %= b; |
a = a % b; |
Example 3: Program to understand Assignment Operators
// Program to understand Assignment operator
#include <iostream>
using namespace std;
int main() {
int a, b, temp;
// 2 is assigned to a
a = 2;
// 5 is assigned to b
b = 5;
// value of a is assigned to temp
temp = a; // temp will be 2
cout << "temp = " << temp << endl;
// assigning the sum of a and b to a
a += b; // a = a +b
cout << "a = " << a << endl;
return 0;
}
Output
temp = 2 a = 7
3. C++ Relational Operators
A relational operator is used to check the relationship between two operands. Relational operator is also known as Comparison operator.
The result of Relational operation is a boolean value that can only be True 1 or False 0.
For example:-
// checks if a is greater than c
a > c;
Here, >
is a relational operator. It checks if a is greater than c or not.
If the relation is true, it returns 1
whereas if the relation is false, it returns 0.
Here, list of relational operator
Operator | Meaning | Example |
---|---|---|
== |
Is Equal To | 2 == 4 gives us false |
!= |
Not Equal To | 2 != 4 gives us true |
> |
Greater Than | 2 > 4 gives us false
|
< |
Less Than | 2 < 4 gives us true
|
>= |
Greater Than or Equal To | 2 >= 4 give us false
|
<= |
Less Than or Equal To | 2 <= 4 gives us true
|
Example 4: Program to understand Relational Operators
// Program to understand Relational Operator
#include <iostream>
using namespace std;
int main() {
int a, b;
a = 2;
b = 4;
bool result;
result = (a == b); // false
cout << "2 == 4 is " << result << endl;
result = (a != b); // true
cout << "2 != 4 is " << result << endl;
result = a > b; // false
cout << "2 > 4 is " << result << endl;
result = a < b; // true
cout << "2 < 4 is " << result << endl;
result = a >= b; // false
cout << "2 >= 4 is " << result << endl;
result = a <= b; // true
cout << "2 <= 4 is " << result << endl;
return 0;
}
Output
2 == 4 is 0 2 != 4 is 1 2 > 4 is 0 2 < 4 is 1 2 >= 4 is 0 2 <= 4 is 1
Note: Relational operators are used in decision making and loops.
4. C++ Logical Operators
Logical operators are used to check whether an expression is true or false. If the expression is true, it returns 1 whereas if the expression is false, it returns 0.
Here, list of Logical Operators
Operator | Example | Meaning |
---|---|---|
&& |
expression1 && expression2 | Logical AND. True only if all the operands are true. |
|| |
expression1 || expression2 | Logical OR. True if at least one of the operands is true. |
! |
!expression | Logical NOT. True only if the operand is false. |
Note: In C++, logical operators are commonly used in decision making.
To further understand the logical operators, let's see the following examples,
Suppose,
a = 2
b = 4
Then,
(a > 2) && (b > 4) evaluates to true
(a > 2) && (b < 4) evaluates to false
(a > 2) || (b > 4) evaluates to true
(a > 2) || (b < 4) evaluates to true
(a < 2) || (b < 4) evaluates to false
!(a == 2) evaluates to true
!(a > 2) evaluates to false
Example 5: Program to understand Logical Operators
// Program to understand Logical Operators
#include <iostream>
using namespace std;
int main() {
bool result;
result = (2 != 4) && (2 < 4); // true
cout << "(2 != 4) && (2 < 4) is " << result << endl;
result = (2 == 4) && (2 < 4); // false
cout << "(2 == 4) && (2 < 4) is " << result << endl;
result = (2 == 4) && (2 > 4); // false
cout << "(2 == 4) && (2 > 4) is " << result << endl;
result = (2 != 4) || (2 < 4); // true
cout << "(2 != 4) || (2 < 4) is " << result << endl;
result = (2 != 4) || (2 > 4); // true
cout << "(2 != 4) || (2 > 4) is " << result << endl;
result = (2 == 4) || (2 > 4); // false
cout << "(2 == 4) || (2 > 4) is " << result << endl;
result = !(4 == 2); // true
cout << "!(4 == 2) is " << result << endl;
result = !(4 == 4); // false
cout << "!(4 == 4) is " << result << endl;
return 0;
}
Output
(2 != 4) && (2 < 4) is 1 (2 == 4) && (2 < 4) is 0 (2 == 4) && (2 > 4) is 0 (2 != 4) || (2 < 4) is 1 (2 != 4) || (2 > 4) is 1 (2 == 4) || (2 < 4) is 0 !(4 == 2) is 1 !(4 == 4) is 0
Explanation of logical operator program
(2 != 4) && (2 < 4)
evaluates to 1 because both operands(2 != 4)
and(2 < 4)
are 1 (true).(2 == 4) && (2 < 4)
evaluates to 0 because the operand(2 == 4)
is 0 (false).(2 == 4) && (2 > 4)
evaluates to 0 because both operands(2 == 4)
and(2 > 4)
are 0 (false).(2 != 4) || (2 < 4)
evaluates to 1 because both operands(2 != 4)
and(2 < 4)
are 1 (true).(2 != 4) || (2 > 4)
evaluates to 1 because the operand(2 != 4)
is 1 (true).(2 == 4) || (2 > 4)
evaluates to 0 because both operands(2 == 4)
and(2 > 4)
are 0 (false).!(4 == 2)
evaluates to 1 because the operand(4 == 2)
is 0 (false).!(4 == 4)
evaluates to 0 because the operand(4 == 4)
is 1 (true).
5. C++ Bitwise Operators
In C++, bitwise operators are used to perform bit-level operations on the operands. The operators are first converted to bit-level and then the calculation is performed on the operands.
The mathematical operations +, -, *, /, etc can be performed at bit-level for faster processing. They can only be used alongside char
and int
data types.
Here, list of Bitwise Operators
Operator | Description |
---|---|
& |
Binary AND |
| |
Binary OR |
^ |
Binary XOR |
~ |
Binary One's Complement |
<< |
Binary Shift Left |
>> |
Binary Shift Right |
Recommended Tutorials:
To learn more, visit C++ bitwise operators.
Apart from the operators we discussed in this tutorial, there are a few other operators, such as sizeof
, ?
, .
, &
, etc., that cannot be neatly classified into one or another type. We will learn more about these operators in later tutorials.
Next Tutorial
We hope that this tutorial helped you develop better understanding of the concept of Operators in C++.
Keep Learning : )
In the next tutorial, you'll learn about C++ Comments
.