gpt4 book ai didi

c - 格式说明符的差异如何影响值?

转载 作者:太空宇宙 更新时间:2023-11-04 08:39:16 25 4
gpt4 key购买 nike

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void)
{
int a = 5;
float b = 10;
double c = 15;
printf("%d %f %lf\n", a, a, a);
printf("%d %f %lf\n", b, b, b);
printf("%f\n", b);
printf("%d %f %lf\n", c, c, c);
printf("%lf\n", c);
}

输出:

5 0.000000 0.000000
0 0.000000 0.000000
10.000000
0 0.000000 0.000000
15.000000

我不明白为什么它在第一种情况下为 b 和 c 打印 0,即使对于正确匹配的格式说明符也是如此?

我正在使用 Visual Studio。

最佳答案

您应该知道 C 编译器不够聪明,无法理解格式字符串并正确转换以下参数。因此,您的论点可能会被错误处理;对于 printf,它们只是堆栈上的一些字节。值为 15.0 的 double 的表示很可能在堆栈上包含几个字节 0,printf 读取为 int,因为您在格式字符串中告诉它这样做。

关于c - 格式说明符的差异如何影响值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24534932/

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