我刚学c! 有个很菜的问题,帮帮我吧!!!

来源:百度知道 编辑:UC知道 时间:2024/05/27 12:07:17
把一个实型按整型输出:
main()
{int i=27;
float j=3.12;
printf("\n=%d,j=%d,if=%f,jf=%f",i,j,i,j);
getch();
}
结果:i=27,j=0,if=0.000000,jf=3.120000

j为什么会是0?就是第二个%d。
不是把实型按整型数出吗?
为什么不是3(去掉小数部分)。

int 型不能用float型输出的
反过来也不行
如果你想让实数型去掉小数部分可以用(float)j,这是强制转换.
至于为什么会那样,估计是规定.
楼主以后编程输出数据时要小心.
下面是C语言开发者说的,权威!
C is not a strongly-typed language, but as it has evolved, its type-checking has been strengthened. The original definition of C frowned on, but permitted, the interchange of pointers and integers; this has long since been eliminated, and the standard now requires the proper declarations and explicit conversions that had already been enforced by good compilers. The new function declarations are another step in this direction. Compilers will warn of most type errors, and there is no automatic conversion of incompatible data types. Nevertheless, C retains the basic philosophy that programmers know what they are doing; it only requires that they state their intentions explicitly.