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
This note was uploaded on 10/05/2011 for the course COMPUTER S 110-1 taught by Professor Tumblin during the Spring '11 term at Northwestern.
- Spring '11
- Computer Science