An operator is a symbol used to perform specific mathematical or logical functions. C++ language has multiple built-in operators.

- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Other Operators

Operator | Description |
---|---|

+ | Adds two operands. |

− | Subtracts second operand from the first. |

* | Multiplies both operands. |

/ | Divides numerator by de-numerator. |

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

++ | Increment operator increases the integer value by one. |

-- | Decrement operator decreases the integer value by one. |

Operator | Description |
---|---|

== | Checks if the values of two operands are equal or not. If yes, then the condition becomes true. |

!= | Checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true. |

> | Checks if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true. |

< | Checks if the value of left operand is less than the value of right operand. If yes, then the condition becomes true. |

>= | Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition becomes true. |

<= | Checks if the value of left operand is less than or equal to the value of right operand. If yes, then the condition becomes true. |

Operator | Description |
---|---|

&& | Called Logical AND operator. If both the operands are non-zero, then the condition becomes true. |

|| | Called Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true. |

! | Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false. |

A | B | A & B | A | B | A ^ B |
---|---|---|---|---|

0 | 0 | 0 | 0 | 0 |

0 | 1 | 0 | 1 | 1 |

1 | 1 | 1 | 1 | 0 |

1 | 0 | 0 | 1 | 1 |

Operator | Example |
---|---|

= | P = M + N will assign the value of M + N to P |

+= | P += Q is equivalent to P = P + Q |

-= | P -= Q is equivalent to P = P - Q |

*= | C *= A is equivalent to C = C * A |

/= | C /= A is equivalent to C = C / A |

%= | C %= A is equivalent to C = C % A |

<<= | C <<= 2 is same as C = C << 2 |

>>= | C >>= 2 is same as C = C >> 2 |

&= | C &= 2 is same as C = C & 2 |

^= | C ^= 2 is same as C = C ^ 2 |

|= | C |= 2 is same as C = C | 2 |

statement = ( sunrise == "East" ) ? true : false ;

Here, "statement" and "sunrise" are two variables. The above code checks if the value stored in "sunrise" is "East", it assigns a value true to the boolean variable "statement" else false is assigned.

Operator precedence determines how an expression is evaluated i.e. in which operator will be evaluated. Higher precedence operator will be evaluated first and then next one.

For example, sum = 8 + 4 * 6; In this expression since * has higher precedence than + operator. sum will be 32.

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 |

← Previous Next →

© 2018 BreakInerview