{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture8-feb6

# lecture8-feb6 - Announcements Lecture 8 Assignment 2 Q&A...

This preview shows pages 1–2. Sign up to view the full content.

Announcements Lecture 8 • Assignment 2 • Topics for today – Last time we looked at conversions within expressions – type conversions in assignment – explicit casting – typedef and sizeof operators – Start arrays 1D 2 Conversions During Assignment • For assignment, the value on the right side of the assignment is converted to the type of the variable on the left side before the assignment is done • Converting from a “larger” type to a “smaller” type may not always produce a meaningful result: int i; float f; i = f; – This assignment is meaningful only if the value of f—when converted to an integer—lies between the smallest and largest values allowed for an int variable. – If f is too large or too small, i will be assigned an apparently meaningless number ( garbage ). • Recall that the conversion of a floating-point number to an integer is done by dropping the fractional part of the number (not by rounding to the nearest integer) 3 Conversion During Assignment i = c; f = i; d = f; i = 842.97; i = -842.97; c = 10000; i = 1.0e20; f = 1.0e100; char c = ‘a’; /*97*/ int i; float f; double d; 4 Casting • Casting is used for explicit type conversion. • A cast expression has the form ( target-type-name ) expression /* it’s a unary operator */ •E x am p l e 1 : float f = 3.5, fracPart; fracPart = f - (int) f; /* fracPart is now 0.5 */ •E x am p l e 2 : int i = 5, j = 2; float f; f = i/j; /* f is now 2.0 */ •f = (float) i/j; /* f is now 2.5 - why ? */ 5

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 3

lecture8-feb6 - Announcements Lecture 8 Assignment 2 Q&A...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online