## 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.