0% found this document useful (0 votes)
15 views12 pages

Lec 11

Uploaded by

vamsi.d124
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views12 pages

Lec 11

Uploaded by

vamsi.d124
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

ESc101 : Fundamental of Computing

I Semester 2008-09

Lecture 11

Types

• Type conversion during assignment


• Type casting
• Many examples : −)

1
Type conversion during assignment statement

2
type conversion in Assignment

i : a variable of type t1
E : an expression (of possibly different type).

what happens when we execute statement i = E; ?


Steps :

1. Evaluate the expression E. Let val be its value and t2 be its type.

2. If t1 is wider than that of t2 :


⇒ : type of val gets promoted to t1 and copied to i;

Otherwise compilation error !!

3
type casting

type cast : an operator to explicitly convert the type of an expression

t : a numeric data type.


E : an expression (possibly of type wider than t1).

(t1)(E)
convert the type of (value of) E into t1.

Note : type cast has higher priority than any arithmetic operator.

4
type casting from wider to narrower range

Information may get lost

1. (int)(1.34) : loss of information (fractional part gets lost)

2. (byte)(1234): loss of information

3. (byte)(123) : no loss of information

5
type casting from narrower to wider
Information may get lost here also

1. long(123421) : no loss of information.

2. (float)123456789987654321L : loss of information


is equal to 1.2345679E17, which is 1.2345679 ∗ 1017

3. (long)((float) 123456789987654321L) : loss of information


is equal to 123456790519087104

Reason : the number of floating points possible using 32 bits float is less than the
number of integers represented by long.

6
type casting in asignment statements

int i; float x = 4; float y=3;

i = (int)(x/y);

What is value of i ?

7
type casting in asignment statements

int i; float x = 4; float y=3;

i = (int)(x/y);

the value of i is 1.

8
(Revisiting) Assigning literals to numeric variables
x = C;
x is a variable of numeric data type, C is a literal of numeric type.

• if x is integer data type :


If C is integer literal, follow the same rule mentioned in Lecture 10,
else compilation error.

• if x is floating point data type :


Same as assigning arithmetic expression to float, so
follow the rule of type conversion inassignment (slide 3)

9
Summary of steps to evaluate expression

Evaluating expression (t)(E)

1. parenthesize E

2. replace variables by their values

3. evaluate the expression (with type cast operator being of


maximumprecedence)

4. let val be the value of expression.

5. apply type cast operator (t) on val

10
Summary of execute assignment statement

i = (t)(E)

1. first evaluate (t)(E)

2. let value be the value of (t)(E).

3. follow the rule of type conversion (slide 3) and copy value into i.

11
For examples and practice problems on types and expression
evaluations, please go through the file practice.pdf available on
course website

12

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy