gpt4 book ai didi

c - 什么时候在 C 中使用 %d 和 %f?

转载 作者:太空宇宙 更新时间:2023-11-04 00:34:09 26 4
gpt4 key购买 nike

我是 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 期望它具有类型 floatdouble

涉及 intfloat 的算术表达式的结果将是 float,因此您需要使用 % f 在这种情况下。

关于c - 什么时候在 C 中使用 %d 和 %f?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55323657/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com