The data type of the variable can be converted. There are two conversion methods: automatic conversion and forced conversion.
Automatic conversion
Automatic conversion occurs when a hybrid operation of different data types is performed, and is automatically completed by the compilation system. Automatic conversion follows the following rules:
1. If the types involved in calculation are different, convert them to the same type before performing calculation.
2. The conversion is performed in the direction of increasing data length to ensure that the accuracy is not reduced. For example, in int and long operations, convert the int value to long before performing operations.
3. All floating-point operations are carried out with Double Precision. Even if only the float Single-precision expressions are included, they must be converted to the double type before calculation.
4. Char and short types must be converted to int type before calculation.
5. In the value assignment operation, the Data Types of the Values on both sides of the value assignment number are different. The type of the value on the right is converted to the type of the value on the left. If the length of the data type on the right is longer than that on the left, part of the data will be lost, which will reduce the precision, and the missing part will be rounded to the forward. Figure 21 shows the rules for automatic type conversion.
Void main ()
{
Float Pi = 3.14159;
Int S, r = 5;
S = r * PI;
Printf ("s = % d/N", S );
}
PI <-- 1, 3.14159
S <-- 0, r <-- 5
S <-- r * pi
Show program running results
Float Pi = 3.14159;
Int S, r = 5;
S = r * PI;
In this example, Pi is real, S, and R is integer. When executing the S = r * PI statement, both the R and PI are converted to the double type, and the result is also the double type. However, because s is an integer, the value is still an integer and the fractional part is removed.
Forced type conversion
Forced type conversion is implemented through the type conversion operation. The general form is (type specifier) (expression). Its function is to forcibly convert the operation result of an expression to the type represented by the type specifier. For example, (float) A converts a to a real (INT) (x + y) and converts x + y to an integer. When using forced conversion, pay attention to the following issues:
1. both the type specifiers and expressions must be enclosed in parentheses (single variables can be left empty). For example, you can write (INT) (x + y) as (INT) X + Y converts X to int type and then adds it to y.
2. both force conversion and automatic conversion are temporary conversions of the Data Length of the variable for the purpose of this operation, without changing the type defined for the variable during data description.
Main ()
{
Float F = 5.75;
Printf ("(INT) F = % d, f = % F/N", (INT) F, F );
}
F <-- 5.75.
Forcibly convert float F to int F float F = 5.75; printf ("(INT) F = % d, f = % F/N", (INT) F, F ); this example shows that although f is forcibly converted to int type, it only plays a role in the operation and is temporary, and the type of F does not change. Therefore, the value of (INT) F is 5 (with decimals deleted), and the value of F is still 5.75.