Knowledge point 4: Type conversion

Type conversion

When different data is mixed, it needs to be converted to the same data type before the operation can be performed.

Automatic type conversion

The automatic type conversion is automatically completed by the compiler, following the following rules:
1. To the data lengthincreaseIn order to ensure that the accuracy of the data does not decrease by
2, the char and short types are first converted to int type during type conversion

short i; int j; char k; 
printf("%d\n", sizeof(i));//2
printf("%d\n", sizeof(j));//4
printf("%d\n", sizeof(i+j));//4
printf("%d\n", sizeof(k+j));//4
printf("%d\n", sizeof(i+k));//4

3. In the assignment operation, no matter whether the data type on both sides of the'=' is high or low, it willThe data type of the lvalueThe data type converted to an rvalue. If the accuracy of the data type on the right is higher than that of the left, it should be truncated or rounded to make it the same as the data type on the left.
Such as:
assign real data to integer variable: discard the decimal part.
Assign integer data to a real variable: the value size does not change, but it is stored in the variable as a floating point number.
Assign character data to an integer variable: Since the character type has only one byte, the 8 bits of the character data are placed in the lower 8 bits of the integer variable. If the highest bit of the character variable is 0, the upper 8 bits of the integer variable are all 0; if the highest bit of the character variable is 1, the upper 8 bits of the integer variable are all 1.
Assign the integer data to the character variable: only put the lower 8 bits in the character variable.

Forced type conversion

x = (x’s type_name) y

Guess you like

Origin blog.csdn.net/Shao_yihao/article/details/112570003