Both C and C++ operators are the symbols used to perform specific mathematical or logical manipulation of data member or functions within the program. C++ Operator has a set of operators to perform different tasks. These are:

**Arithmetic operators:** Arithmetic operators are used to perform the arithmetical or mathematical computations of operands. It can be divided into 2 types. These are:

**Unary operator:**unary operator has a single operand along with the operator.**Binary operator**: Binary operator has 2 operands along with the operator symbol.

Some of the arithmetic operators are as follows:

operator | description | example |
---|---|---|

+ | Used to add 2 operands | C = A + B |

– | Used to subtract second operand from the first operand | C = A – B |

* | Used to multiply 2 operands | C = A * B |

/ | Used to divide first operand by the second operand | C = A / B |

% | Used to find the remainder after an integer division | C = A % B |

++ | Used to increment an integer value by 1 | A++ is equivalent to A = A + 1 |

— | Used to decrement an integer value by 1 | A– is equivalent to A = A – 1 |

**Logical Operators:** the logical operators are used to perform logical operation which gives the Boolean result i.e. either true or false. Here, two or more conditions or constraints are evaluated to get the actual result. Some of these logical operators are:

operator | description | example |
---|---|---|

&& (Logical AND) | If both the operands are non-zero, then condition becomes true. | A && B |

|| (Logical OR) | If any of the operands become non-zero, then condition is true. | A || B |

! (Logical NOT) | It is used to reverse the logical state of the operand. If the operand is true, it makes it false. If the operand is false, it makes it true. | ! (A && B) |

**Relational Operator**: Relational operators are used to compare different values of the operand. These operators are also termed as comparison operator. Some of the relational operators are:

operators | description | example |
---|---|---|

== (equal to) | Used to check the two operands values are equal or not, if equal condition become true | A == B |

!= (not equal to) | Used to check whether two values are equal or not, if not equal then condition become true | A != B |

< (less than) | Used to check whether the left operand is less than right operand, if yes condition become true | A < B |

>(greater than) | Used to check whether the left operand is greater than right operand, if yes condition is true | A > B |

<= (less than or equal to) | Used to check whether the left operand is less than or equal to right operand, if yes condition become true | A <= B |

>= (greater than or equal to) | Used to check whether the left operand is greater than or equal to right operand, if yes condition is true | A >= B |

**Assignment Operator**: Assignment operators are used to assign one operand value to an another operand or modify the current value by performing some operations. Some of the assignment operator are:

operator | description | example |
---|---|---|

= | Used to assign right operand value to the left operand value | A = B |

+= | Used to add both the operand then assign the value to the left operand | A += B is equal to A = A + B |

-= | Used to subtract right operand from the left operand then assign the value to the left operand | A -= B is equal to A = A – B |

*= | Used to multiply both the operand then assign the value to the left operand | A *= B is equal to A = A * B |

/= | Used to divide left operand by the right operand then assign the value to the left operand | A /=B is equal to A = A / B |

%= | Used to divide left operand by the right operand then assign the remainder value to the left operand | A %=B is equal to A = A % B |

**Bitwise Operator**: the bitwise operators are used to perform the operations in the bit level i.e. these operators perform the operation on each bit of the operand. Some of the bitwise operators are:

operators | description | example |
---|---|---|

~ (bitwise NOT) | Used to complement a bit, i.e. if the bit is 0 than make it as 1 or vice-versa | |

& (bitwise AND) | If both the operands are 1 it set the result as 1, otherwise 0 | A & B |

| (bitwise OR) | If one of the operand is 1 it set the result as 1, otherwise 0 | A | B |

^ (bitwise XOR) | If both the operands are same then it set the result as 0, otherwise 1 | A ^ B |

>> (bitwise right shift) | Used to shift specified number of bits towards right | A >> 2 |

<< (bitwise left shift) | Used to shift specified number of bits towards left | A << 2 |

**Conditional Operator**: The conditional operator has the following form **expression1 ? expression2 : expression3** which evaluate the expression1 and if it is true than return expression2 otherwise return expression3. Conditional operator is also termed as ternary operator. For example:

1 |
A > B ? A : B |

Here, if A is greater than B than it will return A as answer, otherwise it will return B.

**Member dereferencing operators**: Member dereferencing operators are used to access the class members through pointers. These operators are:

Operator | description | example |
---|---|---|

::* | Used to declare a pointer to a member of a class | N/A |

* | Used to access a member using object name and a pointer to that member | N/A |

->* | Used to access a member using a pointer to the object and a pointer to that member | N/A |

**Other operator**: In addition to the above operator, C++ also has some other operator as:

operator | description | example |
---|---|---|

<< (insertion operator) | Used with cout function | cout<<endl; |

>> (extraction operator) | Used with cin function | cin>>a; |

, (comma operator) | Used to separate values and expression | (a,b) |

:: (scope resolution operator) | Used to define scope of a variable | A :: B |

sizeof() | Used to find the size of the variable | sizeof(a) |

delete | Used to delete memory used by a variable | delete a |

endl | Used to break the line | Cout<< “A”<<endl; |

new | Used to create an object or to allocate memory space | A = new int; |

## C++ Operator Precedence

An expression may contain more than one operator. The operator precedence defines the way how the operators are evaluated and associativity defines the direction of evaluating the operator. Certain operators have higher precedence than the other operator and they are evaluated first. Here, is the list of the precedence of the operators. The operator at the top has the highest precedence and the operator at the bottom has the lowest precedence.

Precedence group | operator | Associativity |
---|---|---|

Scope | :: | Left-to-right |

Postfix (unary) |
++ — (increment/decrement) |
Left-to-right |

() (functional forms) | ||

[] (subscript) | ||

-> . (member access) | ||

Prefix (unary) |
++ — (increment/decrement) |
Right-to-left |

~ ! (bitwise NOT/logical NOT) | ||

+ – (addition/ subtraction) | ||

& * (reference/ dereference) | ||

new delete (allocation/deallocation) | ||

sizeof | ||

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

Arithmetic | / * % | Left-to-right |

Arithmetic | + – | Left-to-right |

Bitwise 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 |

Assignment | = += -= *= /= %= >>= <<= &= ^= != | Right-to-left |

Sequencing | , | Left-to-right |