C TutorialLesson 9Free preview
C Type Conversion
This lesson is part of C · All levels
Summary
Type conversion is the process of converting one type of data into another type, either implicitly or explicitly by writing or casting. When the operator works with two different types, C performs an integer promotion so that the value of the smaller type rises to the larger type. Casting is done by writing the goal type in front of the value, for example (double) 5 so that the result is decimal. A careless conversion can throw away fractions or cut off a large value.
Key points
- Implicit conversion occurs automatically when operations mix different types
- The explicit conversion (casting) is written with a pattern (type) value
- Dividing two int produces an int, so the decimal part is discarded.
- Storing large value to small types risks loss of data (truncation)
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.