Data Type Conversions
Chapter 4, “Data Types and Expressions,” briefly addressed the fact that sometimes the system implicitly makes conversions when expressions are evaluated. You examined a case with the data types float and int. You saw how an operation that involves a float and an int was carried out as a floating-point operation, with the integer data item automatically converted to a floating point.
You also saw how the type cast operator can be used to explicitly dictate a conversion. So given that total and n are both integer variables
average = (float) total / n;
the value of the variable total is converted to type float before the operation is performed, thereby guaranteeing that the division will be carried out as a floating-point ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access