Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - What is the difference between automatic conversion and forced conversion of data types in C language?
What is the difference between automatic conversion and forced conversion of data types in C language?
C language data type conversion can be divided into automatic conversion and forced conversion.

automatic switching

Automatic conversion occurs when the number of different data types is mixed, which is automatically completed by the compilation system. Automatic conversion follows the following rules:

1. If the operands involved are of different types, convert them to the same type before performing 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.

3. All floating-point operations are performed with double precision. Even if the expression only contains float single precision operation, it should be converted to double type before operation.

4.4. When Char type and short type participate in the operation, they must be converted into int type first.

5. In the assignment operation, when the data types of the quantities on both sides of the assignment number are different, the type of the quantity on the right side of the assignment number will be converted into the type of the quantity on the left. If the data type length of the right quantity is long on the left, some data will be lost, which will reduce the accuracy, and the lost part will be rounded forward by rounding.

Forced type conversion

Forced type conversion is realized by type conversion operation. Its general form is: (type descriptor) (expression) Its function is to forcibly convert the operation result of the expression to the type indicated by the type descriptor. For example: (float) a converts A into a real number type (int)(x+y) and converts the result of x+y into an integer.

The following issues should be paid attention to during forced conversion:

1. Type specifiers and expressions must be enclosed in parentheses (individual variables can be enclosed without parentheses). For example, writing (int)(x+y) as (int)x+y is equivalent to converting x into int and then adding it to y. ..

2. Whether forced conversion or automatic conversion, the data length of the variable is temporarily converted for the needs of this operation, and the type defined for the variable during data description is not changed.

Details are as follows:

char short->; int->; usigned-& gt; long->; double;twofold

Floating-> double;twofold