c++ 类型转换

 正确的变量类型

 强制类型转换

 自动类型转换

 隐式类型转换

 

一、正确的变量类型

     3/2=1.5

     3/2+100.5=102

     

  printf (" 3/2+100.5=%f \n",       3/2 +100.5);

  printf (" 3.0f/2.0f +100.5f=%f \n",  3.0f/2.0f +100.5f);

  printf (" 3.0f/2.0f +100.5f=%f \n",  3.0/2.0 +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

 

posted @ 2016-11-28 10:08  whzym111  阅读(154)  评论(0编辑  收藏  举报