我是 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;
}
输出:
745417384
3.000000
最佳答案
对于 printf
,%d
期望其对应的参数具有类型 int
,其中 %f
期望它具有类型 float
或 double
。
涉及 int
和 float
的算术表达式的结果将是 float
,因此您需要使用 % f
在这种情况下。
关于c - 什么时候在 C 中使用 %d 和 %f?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55323657/