kra*_*ocs 2 c floating-point double printf
我是 C 编程的新手,但在 Java 方面还不错,我的问题是我们什么时候使用 %d 和 %f?在什么情况下?例如,根据下面给出的代码块,如果我被要求取 (int)a*(float)y,我是将它作为 %d 还是作为 %f?
#include <stdio.h>
int main()
{
int a = 2;
float y= 1.5;
printf("%d \n", a*y); //do I take this?
printf("%f", a*y); //do I take this?
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
745417384
3.000000
For printf,%d期望其对应的参数具有 type int,其中%f期望它具有类型floator double。
涉及 andint和 a floatwill的算术表达式的结果是float,因此您需要%f在这种情况下使用。