Exploring type conversion
C provides mechanisms that allow you to convert one type of value into another type of the same value. When there is no loss of precision – in other words, when the conversion of values results in the same value – C operates without complaining. However, when there is a possible loss of precision, or if the resulting value is not identical to the original value, then the C compiler does not provide any such warning.
Understanding implicit type conversion and values
So, what happens when expressions are performed with operands of different types, for example, the multiplication of an int
with a float
, or the subtraction of a double
from a short
?
To answer that, let's revisit our sizes_ranges2.c
program from Chapter 3, Working with Basic Data Types. There, we saw how different data types took different numbers of bytes; some are 1 byte, some are 2 bytes, some are 4 bytes, and most values are 8 bytes.
When C encounters an expression...