Operator Precedence


Operator Precedence

If an expression has more than one operator , it is important to know the order in which they will be applied .

operator precedence


The hierarchy of operator precedence is given below .

Unary operator :

Operator
Meaning
Associativity
-
Unary minus
Right to left
++
Increment
Right to left
--
Decrement
Right to left
!
Logical NOT
Right to left
sizeof
Sizeof
Right to left
(type)
Type definition
Right to left


Arithmetic Operators :

Operator
Meaning
Associativity
*
Multiply
Left to right
/
Divide
Left to right
%
Modulus
Left to right
+
Add
Left to right
-
Subtract
Left to right


Relational Operators :

Operator
Meaning
Associativity
< 
Less than
Left to right
<=
Less than or equal to
Left to right
> 
Greater than
Left to right
>=
Greater than or equal to
Left to right


Equality Operators :

Operator
Meaning
Associativity
==
Equal to
Left to right
!=
Not equal to
Left to right


Logical Operators:

Operator
Meaning
Associativity
&&
Logical AND
Left to right
||
Logical OR
Left to right


Conditional Operator:

Operator
Meaning
Associativity
?:
Conditional OR
Right to left


Assignment Operators:

Operator
Meaning
Associativity
=
Assign
Right to left
+=
Add
Right to left
-=
Subtract
Right to left
*=
Multiply
Right to left
/=
Divide
Right to left
%=
Modulus
Right to left


If x ,y and z are integer variables having the values 2 ,3 and 4 , then the value of the expression x*=2*(y+z)/3  would be .

Note :
The above expression is equivalent to x = x * (-2 * (y + z) /3) .
As the arithmetic operations precedes the assignment operation , the sequence of evaluation is as follows :
Expression evaluated
Result
(y+z)
7
-2 * (y+z) = 2*7
-14
(-2 *(y+z))/3 = -14/3
-4
x * (-2 *(y+z))/3 = 2*4
-8  The final result is 8
       

Thank you …

for more post visit : https://iamafutureprogrammer.blogspot.com
Previous Post
Next Post
Related Posts

0 comments:

Popular Posts