A source of subtle frustrating errors 22 expressions

Info iconThis preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: es for each one. Unintended decision? Unintended Answer! (a source of subtle, frustrating errors) 22 Expressions: Type Promotion Hidden variable's data type is chosen by simple rules: All terms have same type? Then so does the expression's dummy variable. Terms have mixed types? May be trouble! C chooses the `largest' term type (saves data) char int float double ( larger) But sometimes that's not enough: my_float = 5/2; /*result: 2.0 */ 23 SOLUTION: `Type Casting' (pg. 118) FORCES a temporary, local type change (each cast sets type of one term in one expression) Int i,j; float f1,f2,f3; i=5; fl = f2 = f3 = f2 = j=2; i/ j; (float)i/(float)j; (float)(i/j); (float)i/ j; /* /* /* /* result: result: result: lucky!; 2.0 2.5 2.0 2.5 */ */ */ */ `Cast' i a...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online