What Is a Forced Conversion?

When the types of the operands are different and do not belong to the basic data types, it is often necessary to convert the operands to the required type. This process is called a cast. There are two forms of casts: explicit casts and implicit casts.

Coercion

In C, automatic type conversion follows these rules:
1. If the types of calculations involved are different, first convert to the same type and then perform the calculation.
2. Conversion is performed in the direction of increasing data length to ensure that the accuracy does not decrease. Such as int type and long type operation, first convert the int quantity to long type and then perform the operation.
a. If the bytes of the two types are different, convert to a type with a higher number of bytes.
b. If the bytes of the two types are the same, and one is signed and the other is unsigned, they are converted to unsigned types.
3. All floating-point operations are performed in double precision. Even expressions containing only float single-precision quantity calculations must be converted to double before calculation.
4, char type and short type (in visual c ++ and other environments) to participate in the operation, you must first convert to int type.
5. In the assignment operation, when the data types on both sides of the assignment number are different, the type on the right side of the assignment number will be converted to the type on the left side. If the length of the data type on the right is longer than the left, a part of the data will be lost, which will reduce the accuracy and the missing part will be discarded directly.
C ++ language
In practice, when a single-parameter constructor is provided in a class definition, the class provides a method for converting values or variables of other data types into user-defined data types; therefore, it can be said that a single parameter The constructor provides the function of data type conversion.

IN OTHER LANGUAGES

Was this article helpful? Thanks for the feedback Thanks for the feedback

How can we help? How can we help?