一、强制类型转换
printf("3/2+100.5=%f",3/2+100.5);//100.5错误表达
printf (" (float)3/(float)2 +100.5f=%f ", (float)3/(float)2 +100.5f); // (float) 前缀
二、自动类型转换和隐式类型转换
例1:
printf (" 3/2+100.5=%f ", (float)3/2 +100.5f);
printf (" 3/2+100.5=%f ", 3/(float)2 +100.5f);
printf (" 3/2+100.5=%f ", 3/2.0f +100.5f);
printf (" 3/2+100.5=%f ", 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 ", v1,v2,v3,v4);
例3:
int a=3,b=2;
float c=100.5f;
printf (" 3/2+100.5=%f ", a/b+c);
printf (" 3/2+100.5=%f ", (float)a/b+c);
printf (" 3/2+100.5=%f ", a/(float)b+c);
转换顺序 :当低类型和高类型运算时自动转化成高类型进行计算
char ,short,int,long long,float,double