C Conversion Rules
In any C expression when two operands in a binary operation are of different types,
an automatic type conversion will occur. To understand the rules of this conversion process,
you must understand that C types have a specific ordering. The following shows
from left to right the lowest to highest ordering:
char < int < unsigned < long < float < double
The types of the two operands are compared and the lower type operand is converted to the higher type.
The type of the result is the same as the higher type operand.
The following conversion rules are applied in the order listed:
- Any operand of type char is converted to int.
- Any operand of type float is converted to double.
- If either operand is double, then the other is converted to double.
- If either operand is long, then the other is converted to long.
- If either operand is unsigned, then the other is converted to unsigned.
No matter what type expression appears on the right hand side of the assignment
operator, the value is converted to the type of what appears on the left hand side.
Conversion of float to int truncates the fractional part of the number and
long to int drops the excess high order bits. Also, int to char
drops excess high order bits. Conversion such as these can lead to unexpected results
unless you are aware that they are occurring.