In this tutorial, we will learn about the many types of operators in C++. An operator in programming is a symbol that performs operations on a value or variable.
Operators are symbols that carry out operations on variables and values. For instance, the addition operator + is used for addition, while the subtraction operator – is used for subtraction.
Operators in C++ can be classified into 6 types:
- Arithmetic Operators
- Assignment Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Other Operators
1. Arithmetic Operators
In C++ To conduct arithmetic operations on variables and data, arithmetic operators are employed. As an example,
a+b
The + operator is used to combine two variables, a and b. In C++, there are a variety of different arithmetic operators like:
Operator | Operation |
---|---|
+ | Addition |
– | Subtraction |
* | Multiplication |
/ | Division |
% | Modulo Operation (Remainder after division) |
#include <iostream> using namespace std; int main() { int a, b; a = 7; b = 2; cout << "a + b = " << (a + b) << endl; cout << "a - b = " << (a - b) << endl; cout << "a * b = " << (a * b) << endl; cout << "a / b = " << (a / b) << endl; cout << "a % b = " << (a % b) << endl; return 0; }
Output
a + b = 9
a – b = 5
a * b = 14
a / b = 3
a % b = 1
2. Assignment Operators
To assign values to variables in C++, assignment operators are employed. As an example,
: Assign 5 to an a
a = 5;
Here, we’ve given the variable and a value of 5.
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; |
#include <iostream> using namespace std; int main() { int a, b; a = 2; b = 7; cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "\nAfter a += b;" << endl; a += b; // a = a +b cout << "a = " << a << endl; return 0; }
Output
a = 2
b = 7
After a += b;
a = 9
3. Relational Operators
To check the relationship between two operands, a relational operator is utilized. As an example,
a > b;
- Here, > is a relational operator that checks if an is greater than b. It determines whether an is greater than b.
- If the relationship is true, it returns 1, but if it is false, it returns 0.
Operator | Meaning | Example |
---|---|---|
== | Is Equal To | 3 == 5gives us false |
!= | Not Equal To | 3 != 5 gives us true |
> | Greater Than | 3 > 5gives us false |
< | Less Than | 3 < 5 gives us true |
>= | Greater Than or Equal To | 3 >= 5 give us false |
<= | Less Than or Equal To | 3<= 5 gives us true |
#include <iostream> using namespace std; int main() { int a, b; a = 3; b = 5; bool result; result = (a == b); // false cout << "3 == 5 is " << result << endl; result = (a != b); // true cout << "3 != 5 is " << result << endl; result = a > b; // false cout << "3 > 5 is " << result << endl; result = a < b; // true cout << "3 < 5 is " << result << endl; result = a >= b; // false cout << "3 >= 5 is " << result << endl; result = a <= b; // true cout << "3 <= 5 is " << result << endl; return 0; }
Output
3 == 5 is 0
3 != 5 is 1
3 > 5 is 0
3 < 5 is 1 3 >= 5 is 0
3 <= 5 is 1
4. Logical Operators
To determine whether an expression is true or false, logical operators are utilized. If the expression is true, it will return 1, but if it is false, it will return 0.
Operator | Example | Meaning |
---|---|---|
&& | expression1 && expression2 | Logical AND. Only true if all operands are true. |
|| | expression1 || expression2 | Logical OR. If at least one of the operands is true, the result is true. |
! | !expression | Logical NOT. If the operand is false, then true. |
Assume a = 5 and b = 8.
Then,
- True if (a > 3) and (b > 5) are true.
- (a > 3) && (b 5) yields a misleading result.
- (a > 3) || (b > 5) yields true.
- (a > 3) || (b 5) is equal to true.
- (a 3) || (b 5) yields a misleading result.
- The expression!(a 3) evaluates to true.
- !(a > 3) returns false.
#include <iostream> using namespace std; int main() { bool result; result = (3 != 5) && (3 < 5); // true cout << "(3 != 5) && (3 < 5) is " << result << endl; result = (3 == 5) && (3 < 5); // false cout << "(3 == 5) && (3 < 5) is " << result << endl; result = (3 == 5) && (3 > 5); // false cout << "(3 == 5) && (3 > 5) is " << result << endl; result = (3 != 5) || (3 < 5); // true cout << "(3 != 5) || (3 < 5) is " << result << endl; result = (3 != 5) || (3 > 5); // true cout << "(3 != 5) || (3 > 5) is " << result << endl; result = (3 == 5) || (3 > 5); // false cout << "(3 == 5) || (3 > 5) is " << result << endl; result = !(5 == 2); // true cout << "!(5 == 2) is " << result << endl; result = !(5 == 5); // false cout << "!(5 == 5) is " << result << endl; return 0; }
Output
(3 != 5) && (3 < 5) is 1, (3 == 5) && (3 < 5) is 0, (3 == 5) && (3 > 5) is 0,
(3 != 5) || (3 < 5) is 1, (3 != 5) || (3 > 5) is 1,
(3 == 5) || (3 > 5) is 0,
!(5 == 2) is 1,
!(5 == 5) is 0
Explanation
- (3 != 5) && (3 < 5) evaluates to 1 because both operands (3 != 5) and (3 < 5) are 1 (true).
- (3 == 5) && (3 < 5) evaluates to 0 because the operand (3 == 5) is 0 (false)
- (3 == 5) && (3 > 5) evaluates to 0 because both operands (3 == 5) and (3 > 5) are 0 (false).
- (3 != 5) || (3 < 5) evaluates to 1 because both operands (3 != 5) and (3 < 5) are 1 (true).
- (3 != 5) || (3 > 5) evaluates to 1 because the operand (3 != 5) is 1 (true).
- (3 == 5) || (3 > 5) evaluates to 0 because both operands (3 == 5) and (3 > 5) are 0 (false).
- !(5 == 2) evaluates to 1 because the operand (5 == 2) is 0 (false).
- !(5 == 5) evaluates to 0 because the operand (5 == 5) is 1 (true).
5. Bitwise Operator
Bitwise operators are used in C++ to perform operations on single bits. They’re only compatible with char and int data types.
Operator | Description(Binary) |
---|---|
& | AND |
| | OR |
^ | XOR |
~ | One’s Complement |
<< | Shift Left |
>> | Shift Right |
6. Additional C++ Operators
The following is a list of some more common C++ operators. In coming tutorials, we’ll learn more about them.
Operator | Description | Example |
---|---|---|
sizeof | returns the size of data type | sizeof(int); // 4 |
?: | returns value based on the condition | string result = (5 > 0) ? “even” : “odd”; // “even” |
& | represents the memory address of the operand | # // num address |
. | accesses members of struct variables or class objects | s1.marks = 92; |
-> | used with pointers to access the class or struct variables | ptr->marks = 92; |
<< | prints the output value | cout << 5; |
>> | gets the input value | cin >> num; |
Operators Precedence in C++
The order in which terms in an expression are grouped is determined by operator precedence. This has an impact on how an expression is judged. Certain operators take precedence over others; the multiplication operator, for example, takes precedence over the addition operator.
For example, x = 7 + 3 * 2; because operator * has higher precedence than +, x is allocated 13, not 20, and is multiplied by 3*2 before being added to 7.
The highest-priority operators appear at the top of the table, while the lowest-priority operators appear at the bottom. Higher precedence operators will be evaluated first within an expression.
Category | Operator | Associativity |
---|---|---|
Postfix | () [] -> . ++ – – | Left to right |
Unary | + – ! ~ ++ – – (type)* & sizeof | Right to left |
Multiplicative | * / % | Left to right |
Additive | + – | Left to right |
Shift | << >> | Left to right |
Relational | < <= > >= | Left to right |
Equality | == != | Left to right |
Bitwise AND | & | Left to right |
Bitwise XOR | ^ | Left to right |
Bitwise OR | | | Left to right |
Logical AND | && | Left to right |
Logical OR | || | Left to right |
Conditional | ?: | Right to left |
Assignment | = += -= *= /= %=>>= <<= &= ^= |= | Right to left |
Comma | , | Left to right |
You may like:
Input and Output in C++ | C++ Programming
Pingback: C++ switch case statement with Example | C++ Programming