20
Type Conversion Precedence & Associativity - Aakash Singh C Learners

Type Conversion, Precedence and Associativity

Embed Size (px)

Citation preview

Page 1: Type Conversion, Precedence and Associativity

Type ConversionPrecedence & Associativity

- Aakash Singh

C

Learners

Page 2: Type Conversion, Precedence and Associativity

Type Conversion

What is Type Conversion?

• Type Casting or Type Conversion is a way to convert a variable from one data type to another data type.

• For example, if you want to store a long value into a simple integer then you can type cast long to int.

2

Page 3: Type Conversion, Precedence and Associativity

Type Conversion

Types of Type Conversion

• There are two types of type conversion –

1. Implicit Type Conversion

2. Explicit Type Conversion

3

Page 4: Type Conversion, Precedence and Associativity

1. Implicit Type Conversion

What is Implicit Type Conversion?

• C automatically converts any intermediate values to the proper type so that the expression can be evaluated without losing any significance.

• This automatic conversion is known implicit type

conversion.

• If the operands are of different types, the ‘lower type’ is automatically converted to the ‘higher’ type before the operation proceeds.

• The result is of the higher type.

4

Page 5: Type Conversion, Precedence and Associativity

Implicit Type Conversion

short char

int

Unsigned int

long int

Unsigned long int

float

double

long double

Conversion

Hierarchy

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.

This hierarchy

can be used

while evaluating

expressions.

5

Page 6: Type Conversion, Precedence and Associativity

Example of Implicit Type Conversion

6

Page 7: Type Conversion, Precedence and Associativity

Example of Implicit Type Conversion

7

Page 8: Type Conversion, Precedence and Associativity

1. Explicit Type Conversion

What is Explicit Type Conversion?

• The type conversion performed by the programmer by posing the data type of the expression of specific type is known as explicit type conversion.

• The general form of a cast is :

(type-name) expression

• Here, ‘type-name’ is one of the standard C data types and the expression may be a constant, variable, or an expression.

8

Page 9: Type Conversion, Precedence and Associativity

Use of Explicit Type Casting

9

Example Action

X=(int)7.57.5 is converted to integer by truncation.

a = (int)21.3/(int)4.5Evaluated as (21/4) and the result would be 5.

b = (double)sum/n Division is done in floating point mode.

y = (int)(a+b)The result of (a+b) is converted to integer.

z = (int)a+ba is converted to integer and then added to b.

p = cos((double)x) Converts x to double before using it.

Page 10: Type Conversion, Precedence and Associativity

Example of Explicit Type Conversion

10

Page 11: Type Conversion, Precedence and Associativity

Example of Explicit Type Conversion

11

Page 12: Type Conversion, Precedence and Associativity

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.

12

Page 13: Type Conversion, Precedence and Associativity

Precedence & Associativity

13

Precedence rules decides the order in which different operators are applied.

Associativity rule decides the order in which multiple occurrences of the same level operator are applied.

C Operators with their Precedence & Associativity are listed in the following table.

Page 14: Type Conversion, Precedence and Associativity

14

Operator Description Associativity Rank

()[]

Function callArray element reference

Left to right 1

+-++--!~*&sizeof(type)

Unary plusUnary minusIncrementDecrementLogical NegationOnes complementPointer referenceAddressSize of an objectType cast

Right to left 2

*/%

MultiplicationDivisionModulus

Left to right 3

+-

AdditionSubtraction

Left to right 4

<<>>

Left shiftRight shift

Left to right 5

Page 15: Type Conversion, Precedence and Associativity

15

Operator Description Associativity Rank

<<=>>=

Less thanLess than or equal toGreater thanGreater than or equal to

Left to right 6

==!=

EqualityInequality

Left to right 7

& Bitwise AND Left to right 8

^ Bitwise XOR Left to right 9

| Bitwise OR Left to right 10

&& Logical AND Left to right 11

|| Logical OR Left to right 12

?: Conditional expression Right to left 13

Page 16: Type Conversion, Precedence and Associativity

16

Operator Description Associativity Rank

=*= /= %=+= -= &=^= |=<<= >>=

Assignment operators

Right to left 14

, Comma operator Left to right 15

Page 17: Type Conversion, Precedence and Associativity

Summary of operator precedence

17

‘Comma’ operator has lowest precedence.

‘Unary Operators’ are operators having highest precedence.

Operators sharing common block in the above table have equal priority or precedence.

While solving expression, equal priority operators are handled on the basis of FIFO [First in First Out]i.e., Operator Coming First is handled First.

Page 18: Type Conversion, Precedence and Associativity

Example of Precedence

18

Page 19: Type Conversion, Precedence and Associativity

Example of Associativity

19

Page 20: Type Conversion, Precedence and Associativity

20

Please give your precious feedbacks and suggestions at

[email protected]