An operator is specially a symbol that tells compiler to perform specific manipulation. C++ contains various different types of operators.
Types of C++ Operators
- Arithmetic Operators
- Logical Operators
- Relational Operators
- Assignment Operators
- Bitwise Operators
- Misc. Operators
Arithmetic operators are used to perform basic mathematical operation such as addition (+), subtraction (-), multiplication (*), division (/) etc.
Let’s assume two variables A and B containing value 5 and 10 respectively, performs following arithmetic operations.
|+||Add operation||A+B gives 15|
|–||Subtract second operand from first||A-B gives -5|
|*||Multiply operand||A*B gives 50|
|/||Divides numerator by de-numerator||B/A gives 2|
|%||Modulus operation||B%A gives 0|
Logical operators are used to check condition between operands such as AND (&&), OR (||) and NOT (!).
AND (&&) Operator
&& operator returns true value if all operand are true else it returns false.
OR (||) Operator
|| operator returns true value if any one operand are true else it returns false.
NOT (!) Operator
! operator returns inverse result. If condition is true it returns false and if condition is false it returns true.
Relational operator shows the relation and compare between operands. Following are the different relational operations.
Let’s assume two variables A and B containing value 5 and 10 respectively, performs following relational operations.
|==||Check equality of both operands||(A==B) false|
|!=||Check first operand is not equal to second||(A!=B) true|
|<||Check first operand is less than second||(A<B) true|
|>||Check first operand is greater than second||(A>B) false|
|<=||Check first operand is less or equal to second||(A<=B) true|
|>=||Check first operand is greater or equal to second||(A>=B)false|
Assignment Operators (=)
Assignment operator “=” assigns a value to another variable. The assignment of value takes from right to left.
|=||It simply assigns a value to variable||A = 5;|
|+=||Add and assign||A += B; is equivalent to A=A+B;|
|-=||Subtract and assign||A -= B; is equivalent to A=A-B;|
|*=||Multiply and assign||A *=B; is equivalent to A=A*B;|
|/=||Divide and assign||A /=B; is equivalent to A=A/B;|
Bitwise operator works on bit. It is used to change individual bits into a number. Bitwise operators modify variable according to bit patterns. Following are the different bitwise operators:
|<<||Bitwise left shift|
|>>||Bitwise right shift|
Some other misc. operators are:
|cast||Type cast operator|
|Condition ? A : B||Conditional operator|
Precedence of C++ Operators
The precedence of operator determines the sequence of operator to evaluate first. The higher precedence operator evaluate first and next.
For example a statement x = 5 + 6 * 2; gives 17 not 22. This expression perform multiplication (*) operation first then addition (+), as multiplication precedence is higher than addition.
Following table describe precedence of different operator.
|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|