Q20. What is operator precedence? Explain with an example.
Answer:
Each operator in C++ has a precedence associated with it. This precedence is used to determine, the evaluation of each expression involving more than one operator. The operator with higher level of precedence is evaluated first and the operators of the same precedence are evaluated either from left to right or from right to left depending upon the level.
The below table depicts the precedence of operators from higher to lower,
Operator
|
Description
|
::
|
Scope resolution
|
.→
|
Member access
|
( )
|
Parenthesis
|
[ ]
|
Array subscript
|
++
|
Post increment
|
--
|
Post decrement
|
++
|
Preincrement
|
--
|
Predecrement
|
~
|
Tilde
|
!
|
Logical NOT
|
+
|
Unary increment
|
-
|
Unary decrement
|
*
|
Unary multiplication
|
&
|
Address of
|
(type)
|
Typecasting
|
sizeOf
|
Size in bytes
|
new
|
Dynamic memory
allocation
|
delete
|
Dynamic memory release
|
.*,→*
|
Pointer to member
|
*
|
Multiplication
|
/
|
Division
|
%
|
Modulo division
|
+
|
Addition
|
-
|
Subtraction
|
<<
|
Left shift
|
>>
|
Right shift
|
<
|
Less than
|
<=
|
Less than or equal to
|
>
|
Greater than
|
>=
|
Greater than or equal
to
|
=
|
Equal to
|
!=
|
Not equal to
|
&
|
Bitwise AND
|
^
|
Bitwise exclusive OR
|
|
|
Bitwise OR
|
&&
|
Logical AND
|
||
|
Logical OR
|
?:
|
Conditional
|
=
|
Equal
|
*=
|
Multiplication
assignment
|
/=
|
Division assignment
|
%=
|
Modulus assignment
|
+=,-+
|
Left to right
|
<<=
|
Bitwise shift left
assignment
|
>>=
|
Bitwise shift right
assignment
|
& = , | = ,
^ =
|
Bitwise AND, OR, XOR
assignment (L to R)
|