Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - How does C language realize the forced conversion of data types?
How does C language realize the forced conversion of data types?
Forced type conversion

When the types of operands are different and do not belong to the basic data type, it is often necessary to force type conversion to convert the operands into the required types. There are two forms of modeling, called explicit modeling and implicit modeling.

1, explicit conversion

Explicit casts require a cast operator of the following format:

Type (< expression & gt)

or

(Type)< Expression & gt

Where type is a type descriptor, such as int, float, etc. & lt expression & gt is an expression. 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. For example:

int nVar = 0x ab 65;

char cChar = char(nVar);

The result of the above forced type conversion is to delete the upper two bytes of the integer value 0xab65 and assign the lower two bytes to the variable cChar as the Char type value, but the value of nVar has not changed after the type conversion.

2. Implicit cast

Implicit type conversion occurs in assignment expressions and function call expressions with return values. In an assignment expression, if the types of operands on the left and right sides of the assignment operator are different, the operands on the right side of the assignment operator are forcibly converted to the type values on the left side of the assignment operator, and then assigned to the variables on the left side of the assignment operator. When calling a function, if the type of the returned expression is different from the type of the function return value, the value of the returned expression will be forcibly converted to the type of the function return value when returning the value, and then the value will be returned, for example:

int nVar

Double dVar = 3.88

NVar = dVar// After executing this sentence, the value of nvar is 3, while the value of dVar is still 3.88.

1, if x and y are known to be double precision types, what is the result of the expressions: x= 1, y=x+3/2?

Because both X and Y are double types, after executing the statement y=x+3/2, the small tree part will be output. The default of double type is that I forgot the decimal places. You will understand after debugging yourself!