The execution process of the expression: firstly, the expression performs forced type conversion on f 1, (int)f 1=2, so that the value of f 1 is converted into an integer, and then f2 is added.
F2 is a floating-point number. During the operation, the integer is upgraded to floating point number, then 2 becomes floating point number 2.0, and then added to 2.5 to get the result of 4.5.
Extended data:
When the types of operands are different and do not belong to the basic data types, it is often necessary to convert the operands to the required types, which is the process of forced type conversion. There are two forms of modeling: explicit modeling and implicit modeling.
In C language, automatic type conversion follows the following rules:
1. If the operands involved are of different types, first convert them to the same type, and then perform the operation.
2. Transform in the direction of increasing the data length to ensure that the accuracy does not decrease. Such as int and long operations, the int quantity is converted into long before the operation.
A. If the number of bytes of the two types is different, convert to the type with higher number of bytes.
B, if the two types have the same number of bytes, and one is signed and the other is unsigned, it will be converted to unsigned type.
3. All floating-point operations are performed with double precision. Even expressions that only contain floating-point single-precision operations should be converted to double-precision types before operations.
Explicit conversion type:
The explicit cast in C is simple, and the format is as follows:
Type b = (type) a;
Where TYPE is a type descriptor, such as int, float, etc. A numeric value of type is returned after a cast operator operation is forced. This forced type conversion operation will not change the operand itself, and the operand itself will not change after the operation.
Baidu Encyclopedia-Forced Type Conversion