1 / 8

Type Conversion

Type Conversion. Type Conversion. C provides two methods of changing the type of an expression: Type conversion (done implicitly) Cast expressions (done explicitly) Examples of implicit conversion: float x; int i ; if (x < i ) ... /* Example 1 */ x = i ; /* Example 2 */

Télécharger la présentation

Type Conversion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Type Conversion

  2. Type Conversion • C provides two methods of changing the type of an expression: Type conversion (done implicitly) Cast expressions (done explicitly) • Examples of implicit conversion: float x; inti; if (x < i) ... /* Example 1 */ x = i; /* Example 2 */ In each case, i is implicitly converted to float type.

  3. The Usual Arithmetic Conversions • The usual arithmetic conversions are applied to operands in an arithmetic or logical expression. • Operands are converted to the “smallest” type that will safely accommodate both values. • This is often done by converting (promoting) the operand of the “smaller” type to the type of the other operand.

  4. The Usual Arithmetic Conversions • Examples of the usual arithmetic conversions: char c; short s; inti; long int l; float f; double d; long double ld; i = i + c; /* c is converted to int */ i = i + s; /* s is converted to int */ l = l + i; /* i is converted to long */ f = f + l; /* l is converted to float */ d = d + f; /* f is converted to double */ ld = ld + d; /* d is converted to long double */

  5. Conversion During Assignment • The usual arithmetic conversions do not apply to assignment. Instead, the value on the right side of the assignment is converted to the type of the variable on the left side. • Warning: Converting from a “large” type to a “small” type may not always produce a meaningful result: inti; 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.

  6. Conversion During Assignment • 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): inti; i = 842.97; /* i is now 842 */ i = -842.97; /* i is now -842 */

  7. Casting • Forces one type into another • For example, yourFloat= (float) myinteger; • Will force the integer myInteger into a float, yourFloat • iMyInteger = (int) fYourFloat ; • Will work too, but you’ll lose the decimal portion • i.e. 1.9 forced into an integer will result in 1 Introduction to Computers and Programming - Class 5

  8. Rules of Promotion (cont’d) Use a Cast Operator int num = 4; float avg = ( 90 + 92 + 95 + 100 ) / (float) num; In this case, num is explicitly cast to a float. And, because we have one float, everything else is promoted.

More Related