int + double
The int is converted to a double before the addition takes place. The result is a double.
Understanding how C handles mixed data types automatically, and how to force conversions safely.
5 / 2 and assign it to a float?When evaluating mixed-type expressions, C automatically "promotes" smaller or less precise types to match the largest/most precise type involved to avoid data loss.
long doubledoublefloatunsigned long longlong longlongunsigned intintThe int is converted to a double before the addition takes place. The result is a double.
The char is promoted to an int (using its ASCII value) before addition.
Use the cast operator (type) to explicitly force an expression to become a specific type.
(float) has higher precedence than division.7 to 7.0f.2 to 2.0f.Converting a larger or more precise type into a smaller or less precise type often results in data loss or truncation.
Assigning a floating-point number to an integer drops the fractional part entirely. It does NOT round.
A char typically holds 8 bits (-128 to 127). Forcing a larger integer into it chops off the higher-order bits.
Moving from a smaller type (int) to a larger type (double) happens automatically and preserves data.
Moving from a larger type to a smaller type forces data loss, truncation, or unexpected overflow.
Use the (type) cast operator to clarify your intentions, especially to prevent integer division traps.