一、强制类型转换
printf("3/2+100.5=%f",3/2+100.5);//100.5错误表达
printf (" (float)3/(float)2 +100.5f=%f \n", (float)3/(float)2 +100.5f); // (float) 前缀
二、自动类型转换和隐式类型转换
例1:
printf (" 3/2+100.5=%f \n", (float)3/2 +100.5f);
printf (" 3/2+100.5=%f \n", 3/(float)2 +100.5f);
printf (" 3/2+100.5=%f \n", 3/2.0f +100.5f);
printf (" 3/2+100.5=%f \n", 3.0f/2 +100.5f);
例2:
char v1=2;
short v2=111111;
float v3=3.45;
double v4=10.0f /3;
v1=v2;
printf ("v1=%hd,v2=%hd,v3=%f,v4=%f \n", v1,v2,v3,v4);
例3:
int a=3,b=2;
float c=100.5f;
printf (" 3/2+100.5=%f \n", a/b+c);
printf (" 3/2+100.5=%f \n", (float)a/b+c);
printf (" 3/2+100.5=%f \n", a/(float)b+c);
转换顺序 :当低类型和高类型运算时自动转化成高类型进行计算
char ,short,int,long long,float,double
时间: 2024-10-08 09:33:53