C ProgType Conversion, Precedence and Associativity
C ProgType Conversion, Precedence and Associativity
2
Type Conversion
3
1. Implicit Type Conversion
4
Implicit Type Conversion
C uses rule that, in all expressions except assignments, any implicit type
conversions are made from a lower size to a higher size type as shown
below.
6
1. Explicit Type Conversion
7
Use of Explicit Type Casting
Example Action
7.5 is converted to integer by
X=(int)7.5
truncation.
Evaluated as (21/4) and the result
a = (int)21.3/(int)4.5
would be 5.
8
Example of Explicit Type Conversion
9
Example of Explicit Type Conversion
10
Precedence & Associativity
• What is Precedence?
– C has a precedence associated with it. This precedence is used to
determine how an expression involving more than one operator is
evaluated.
• What is Associativity?
– The operators at higher level of precedence are evaluated first.
– The operators of the same precedence are evaluated either from ‘left
to right’ or from ‘right to left’ depending on the level. This is known as
the associativity property of an operator.
11
Precedence & Associativity
12
Operator Description Associativity Rank
14
Operator Description Associativity Rank
= Assignment operators Right to left 14
*= /= %=
+= -= &=
^= |=
<<= >>=
15
Precedence
Example
16
Example of Associativity
Associativity
Example
17