C Language Type Conversion Rules Explained
The C language type conversion rule refers to the compiler automatically converting data types in expressions so that operands of different types can participate in operations, ensuring that operands have the same type for calculations.
Here are some fundamental principles of automatic type conversion rules in C.
- If the types of the two operands are different, one of the types will be converted to match the other type, so that both operands have the same type. The conversion rules are as follows:
- If one of the operands is of type long double, the other operand will be converted to long double type.
- If one of the operands is of type double, the other operand will be converted to double type.
- If one of the operands is a float type, the other operand will be converted to a float type.
- If one of the operands is of type unsigned long int and the other operand is of type long int, the long int type will be converted to unsigned long int type.
- If one operand is of unsigned int type and the other operand is of int type, the int type will be converted to unsigned int type.
- If one operand is of type long int and the other operand is of type int, the int type will be converted to long int type.
- When one operand is a signed type and the other operand is an unsigned type, the following conversion will occur:
- If the range of a signed type can accommodate all the non-negative numbers of an unsigned type, then the unsigned type will be converted to a signed type.
- Otherwise, signed types will be converted to unsigned types.
- When one operand is of type float and the other operand is of type double, the float type will be converted to double type.
- When one operand is of type long double and the other is of type double, the double type will be converted to long double type.
- When one operand is of type long double and the other operand is of type float, the float type will be converted to long double type.
It is important to note that type conversion only occurs within an expression and does not change the type of the operands themselves. The resulting converted value will be used for calculations and assignments based on its new type.