一、强制数据类型转换举例
1.1 double赋值给int
#include<stdio.h>
int main()
{double sum = 18, count = 5;int mean;mean = sum / count;printf("Value of mean : %d\n", mean);}
执行结果:
double赋值给int,小数部分会删除,会导致数据丢失
1.2 未进行强制数据类型转换
#include<stdio.h>
int main()
{int sum = 18, count = 5;double mean;mean = sum / count;printf("Value of sum / count : %d\n", (sum / count));printf("Value of mean : %d\n", mean);}
执行结果:
未进行类型强制转换,整型赋值给double,结果为0
1.3 进行强制数据类型转换
#include<stdio.h>
int main()
{int sum = 18, count = 5;double mean;mean = (double)sum / count;printf("Value of sum / count : %d\n", (sum / count));printf("Value of mean : %f\n", mean);}
执行结果:
强制转为double,输出正确结果
二、整数提升
三、常用的算术转换
整数强制转换为浮点数