• c++ 类型转换


     正确的变量类型
    
     强制类型转换
    
     自动类型转换
    
     隐式类型转换
    
     
    
    一、正确的变量类型
    
         3/2=1.5
    
         3/2+100.5=102
    
         
    
      printf (" 3/2+100.5=%f 
    ",       3/2 +100.5);
    
      printf (" 3.0f/2.0f +100.5f=%f 
    ",  3.0f/2.0f +100.5f);
    
      printf (" 3.0f/2.0f +100.5f=%f 
    ",  3.0/2.0 +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
  • 相关阅读:
    ZROI2018提高day9t1
    p2114 起床困难综合症
    EZOJ #78
    Linux JDK配置
    jps命令
    虚拟机 网卡模式配置
    redhat下yum命令安装(替换为centos yum命令)
    URL编码
    Query DSL(2)----Full text queries
    Query DSL(1)
  • 原文地址:https://www.cnblogs.com/whzym111/p/6108390.html
Copyright © 2020-2023  润新知