In this section of the **C++ tutorial**, we will have a look at various operators in C++. An operator in C++ program tells the compiler to do particular arithmetical or logical operations.

## Operators in C++

To perform any kind of mathematical or local operation within a C++ program operator are used. Each type of operator has a specific symbol like to perform the addition of two integers ‘+’ is used.

Now let’s have a look at different types of operators that are supported by C++ programming language.

## Types of operators

### Assignment Operator

Operates ‘ = ‘ is utilized for assignment, takes the right-hand side (called rvalue) and copies it to the left-hand side (called lvalue). The assignment operator is the only operator that can be overloaded but can not be inherited.

### Mathematical Operators

Symbol of Operators |
Description |

+ | It performs addition. |

– | It performs subtraction. |

* | It performs multiplication. |

/ | It divides numerator by de-numerator |

% | Modulus Operator and the remainder of after an integer division |

++ | Increment operator increments the value by 1. |

— | Decrement operator decrement the value by 1. |

### Relational Operators

Symbol of Operators |
Description |

== | It checks whether or not the values of two operands are equal, if yes, then the condition becomes true. |

!= | It checks whether or not two operands ‘ values are equal, if values are not equal, then the condition becomes true. |

> | It checks if the left operand’s value is greater than the right operand’s value, if yes, then the statement becomes valid. |

< | It checks if the left operand’s value is less than the right operand’s value, if yes, then the condition becomes valid. |

>= | It checks if the left operand’s value is greater than or equal to the right operand’s value, if yes then the condition becomes true. |

<= | It checks if the left operand’s value is less than or equal to the right operand’s value, if yes then the condition is valid. |

### Logical Operators

Symbol of Operators |
Description |

&& | It is called Logical AND operator. If both operands are non-zero, the statement will become valid. |

|| | It is called the Logical OR Operator. If either of the two operands is non-zero, the condition will become true. |

! | It is called Logical NOT Operator. It is used to reverse its operand’s logical state. If a statement is valid, then the operator of Logical NOT is incorrect. |

### Bitwise Operators

The Bitwise operators are used to work on the operands at bit-level. First, the operators are converted to bit-level and then the operands are calculated. For faster processing, mathematical operations such as addition, subtraction, multiplication, etc. can be carried out at bit-level.

**Other Operators**

In addition to the operators listed above, some other operators are available in C++ which are used to perform those specific tasks. Some of them will be discussed here:

**sizeof operator**: It is a unary compile time operator that can be used to calculate its operand’s size. The value of sizeof is the unsigned integral form usually referred to as size t. Essentially, the operator’s value is used to measure the factor size.**Comma Operator**: It is a binary operator evaluating its first operand and discarding the result, then evaluating the second operand and returning this (and type) value. The comma operator has any C operator’s lowest precedence. Comma acts as an operator as well as a separator.**Conditional Operator**: It is denoted like this:*Expression1 ? Expression2 : Expression3*. Here, the condition to be determined is Expression1. If the condition(Expression1) is true thenExpression2 results will be executed and returned otherwise if the condition(Expression1) is false thenExpression3 results will be executed and returned. We can replace conditional operators with the use of if.. else statements.

## C++ Operator Precedence

The following table lists the C++ operators ‘ order and associativity. In descending precedence, operators are classified top to bottom.

Precedence | Operator | Description | Associativity |
---|---|---|---|

1 | :: | Scope resolution | Left-to-right |

2 | ++ — | Suffix/postfix increment and decrement | |

type() type{} | Function-style type cast | ||

() | Function call | ||

[] | Array subscripting | ||

. | Element selection by reference | ||

-> | Element selection through pointer | ||

3 | ++ — | Prefix increment and decrement | Right-to-left |

+ – | Unary plus and minus | ||

! ~ | Logical NOT and bitwise NOT | ||

(type) | C-style type cast | ||

* | Indirection (dereference) | ||

& | Address-of | ||

sizeof | Size-of[note 1] | ||

new, new[] | Dynamic memory allocation | ||

delete, delete[] | Dynamic memory deallocation | ||

4 | .* ->* | Pointer to member | Left-to-right |

5 | * / % | Multiplication, division, and remainder | |

6 | + – | Addition and subtraction | |

7 | << >> | Bitwise left shift and right shift | |

8 | < <= | For relational operators < and ≤ respectively | |

> >= | For relational operators > and ≥ respectively | ||

9 | == != | For relational = and ≠ respectively | |

10 | & | Bitwise AND | |

11 | ^ | Bitwise XOR (exclusive or) | |

12 | | | Bitwise OR (inclusive or) | |

13 | && | Logical AND | |

14 | || | Logical OR | |

15 | ?: | Ternary conditional[note 2] | Right-to-left |

= | Direct assignment (provided by default for C++ classes) | ||

+= -= | Assignment by sum and difference | ||

*= /= %= | Assignment by product, quotient, and remainder | ||

<<= >>= | Assignment by bitwise left shift and right shift | ||

&= ^= |= | Assignment by bitwise AND, XOR, and OR | ||

16 | throw | Throw operator (for exceptions) | |

17 | , | Comma | Left-to-right |