Chinaunix

标题: 关于隐式类型转换的问题. [打印本页]

作者: pro21ms4    时间: 2008-02-02 19:21
标题: 关于隐式类型转换的问题.
printf("%f\n", (float)(90/100));   
    这个是0, 不用说.
printf("%f\n", 90/100.0)
    这个90由于 类型提升 成float, 得0.9
printf("%f\n", 90/100)
    这个 为什么就成 不确定了?
作者: lenovo    时间: 2008-02-02 21:37
%f指定的是float,
而90/100结果是整数,
它们类型不匹配。
作者: cugb_cat    时间: 2008-02-02 22:02
应该是类型提示的原因。
作者: xi2008wang    时间: 2008-02-02 22:34
是printf中格式控制字符串中的类型与常量类型不匹配导致的
这已经不是类型转换的问题了

  1. [root@mylinux my]# vi temp.c
  2. #include    <stdio.h>

  3. int
  4. main(void)
  5. {
  6.     printf("%d\n", 2.1);
  7.     printf("%f\n", 2);
  8.     return 0;
  9. }
  10. [root@mylinux my]# gcc temp.c   
  11. [root@mylinux my]# ./a.out
  12. -858993459
  13. 2.099998
复制代码

作者: william.zhang    时间: 2008-02-03 10:47
printf 函数中如果类型不匹配结果难以预测:wink:




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2