Operators are mainly considered the foundation of any programming language. Operators are symbols that enable us to carry out particular logical and mathematical operations on operands. In other words, an operator controls the operands. For example, the operator '+' is used for addition, as shown below:
c = a + b;
Here, "a" and "b" are the operands, and "+" is the operator, also known as the addition operator. The addition operator tells the compiler to combine which means to add the operands 'a' and 'b’.
In this tutorial, with the help of some examples, we will learn about the different types of operators in C++. An operator is a symbol in programming that performs an operation on a value or variable.
Operators are symbols that operate on variables and values. For example, + is an addition operator, whereas - is a subtraction operator.
C++ operators are mainly classified into six types:
To perform arithmetic operations on variables and data, arithmetic operators are used. As an example,
a + b
The + operator is used in order to add two variables, a and b. In C++, there are numerous other arithmetic operators.
Arithmetic Operations | Operator | Use | Description |
---|---|---|---|
Addition | + | (A+B) | Adds two operands A & B |
Subtraction | - | (A-B) | Subtracts operand B from A |
Multiplication | * | (A*B) | Multiply two operands A & B |
Division | / | (A/B) | Divides two operands A & B |
Modulus | % | (A%B) | Get remainder after division of operand A by operand B |
#include <iostream>
using namespace std;
int main() {
int a, b;
a = 5;
b = 2;
// here printing of the sum of a and b takes place
cout << "a + b = " << (a + b) << endl;
// difference of a and b is printing
cout << "a - b = " << (a - b) << endl;
// the product of a and b is printing
cout << "a * b = " << (a * b) << endl;
// the division of a by b is printing
cout << "a / b = " << (a / b) << endl;
// the modulo of a by b is printing
cout << "a % b = " << (a % b) << endl;
return 0;
}
Output:
a + b = 7 a - b = 3 a * b = 10 a / b = 2 a % b = 1
As expected, the operators +, -, and * compute addition, subtraction, and multiplication, respectively.
In our program, take note of the operation (a / b). The division operator is the / operator.
As shown in the preceding example, by dividing an integer by another integer then we will be getting the quotient. However, if either the divisor or the dividend is a floating-point number, the result will be in decimals.
Note: In C++, the output may vary based on combinations of operands in different datatypes ( int/double ). Example following would be printed if operands as changed as -
5/2 is 2
5.0 / 2 is 2.5
5 / 2.0 is 2.5
5.0 / 2.0 is 2.5
The remainder is computed using the modulo operator %. The remainder is 1 when a = 9 is divided by b = 4.
Please keep in mind that the % operator can only be used with integers.
Unary operators are those that operate or work with a single operand. As an example: Decrement(--) and Increment(++) Operators
That is the increment and decrement operators in C++ are ++ and --, respectively.
++ increases the operand's value by 1 and -- decreases it by 1.
For an example
int num = 6;
// increment operator
++num; // 7
The code ++num; here increases the value of num by 1.
//An example of the working of increment and decrement operators
#include <iostream>
using namespace std;
int main() {
int a = 20, b = 200, result_a, result_b;
// here we are incrementing a by 1 and we will store the result in result_a
result_a = ++a;
cout << "result_a = " << result_a << endl;
//here we are decrementing b by 1 and we will store the result in result_b
result_b = --b;
cout << "result_b = " << result_b << endl;
return 0;
}
Output:
result_a = 21 result_b = 199
The ++ and -- operators were used as prefixes in the preceding program (++a and —b). However, these operators can also be used as postfixes (a++ and b--).
Assignment operators are used in C++ to assign values to variables.
For example,
// assign 10 to b
b = 10;
The variable b has been assigned a value of 10 in the above example.
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; |
//An example for the assignment operators
#include <iostream>
using namespace std;
int main() {
int a, b;
// here the 6 is assigned to a
a = 6;
// here the 12 is assigned to b
b = 12;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "\nAfter a += b;" << endl;
// here we are assigning the sum of a and b to a
a += b; // a = a +b
cout << "a = " << a << endl;
return 0;
}
Output:
a = 6 b = 12 After a += b; a = 18
To check the relationship between two operands, a relational operator is used. For example,
// check whether a is greater than b
a > b;
In this case, > is a relational operator. It helps to check whether a is greater than b.
If the relation is true, it returns 1, and if it is false, it returns 0.
Operator | Meaning | Example |
---|---|---|
== | Is Equal To | 5 == 7 gives us false |
!= | Not Equal To | 5 != 7 gives us true |
> | Greater Than | 5 > 7 gives us false |
< | Less Than | 5 < 7 gives us true |
>= | Greater Than or Equal To | 5 >= 7 give us false |
<= | Less Than or Equal To | 5 <= 7 gives us true |
#include <iostream>
using namespace std;
int main() {
int a, b;
a = 5;
b = 7;
bool result;
result = (a == b); // false
cout << "5 == 7 is " << result << endl;
result = (a != b); // true
cout << "5 != 7 is " << result << endl;
result = a > b; // false
cout << "5 > 7 is " << result << endl;
result = a < b; // true
cout << "5 < 7 is " << result << endl;
result = a >= b; // false
cout << "5 >= 7 is " << result << endl;
result = a <= b; // true
cout << "5 <= 7 is " << result << endl;
return 0;
}
Output:
5 == 7 is 0 5 != 7 is 1 5 > 7 is 0 5 < 7 is 1 5 >= 7 is 0 5 <= 7 is 1
Logical operators are often used to check whether an expression is true or false. If the expression is true, it will return 1, and if it is false, then it returns 0.
Operator | Example | Meaning |
---|---|---|
&& | expression1 && expression2 | Logical AND. It will be true if and only if all the operands are true. |
|| | expression1 || expression2 | Logical OR. It will be true if at least one of the operands should is true |
! | !expression | Logical NOT. it will be true if and only if the operand is false. |
Logical operators are commonly used for decision-making in C++. Let's look at some examples to fully understand the logical operators.
#include <iostream>
using namespace std;
int main()
{
bool result;
int a = 4, b = 6;
result = (a != b) && (a < b); // true
cout << "(a != b) && (a < b) is " << result << endl;
result = (a == b) && (a < b); // false
cout << "(a == b) && (a < b) is " << result << endl;
result = (a == b) && (a > b); // false
cout << "(a == b) && (a > b) is " << result << endl;
result = (a != b) || (a < b); // true
cout << "(a != b) || (a < b) is " << result << endl;
result = (a != b) || (a > b); // true
cout << "(a != b) || (a > b) is " << result << endl;
result = (a == b) || (a > b); // false
cout << "(a == b) || (a > b) is " << result << endl;
result = !(b == 3); // true
cout << "!(b == 3) is " << result << endl;
result = !(b == b); // false
cout << "!(b == b) is " << result << endl;
return 0;
}
Output:
(a != b) && (a < b) is 1 (a == b) && (a < b) is 0 (a == b) && (a > b) is 0 (a != b) || (a < b) is 1 (a != b) || (a > b) is 1 (a == b) || (a > b) is 0 !(b == 3) is 1 !(b == b) is 0
Logic operator program explanation
Bitwise operators in C++ operate on integer data at the individual bit level. These operations include bit testing, bit setting, and bit shifting. A detailed explanation of bitwise operators in C++ is given in the next tutorial.
operator | Its description |
---|---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR |
~ | Bitwise ONE’S complement |
<< | Bitwise shift left |
>> | Bitwise shift right |
Here is a list of some other common C++ operators. Subsequent tutorials will cover them in detail.
Operator | Description | Example |
---|---|---|
sizeof | The size of the data type will be returned by it. | sizeof(int); // 5 |
?: | based on the condition, returns a value | string result = (7 > 0) ? "even" : "odd"; // "even" |
& | represents the operand's memory address. | # // address of num |
. | members of struct variables or class objects are accessed | s1.marks = 94; |
-> | used to access the class or struct variables with pointers | ptr->marks = 94; |
<< | the result value is printed | cout << 6; |
>> | obtains the input value | cin >> num; |