gpt4 book ai didi

c - fprintf 产生不同的输出

转载 作者:太空宇宙 更新时间:2023-11-03 23:49:23 24 4
gpt4 key购买 nike

我在 C++ Builder 3 和 C++ Builder 5 中编译了下一个程序,它产生了不同的输出:

#include <vcl.h>
#include <stdio.h>
#pragma hdrstop

//---------------------------------------------------------------------------

#pragma argsused
int main(int argc, char* argv[])
{
const char filename[] = "C://fprintf-test.txt";
FILE *file = fopen(filename, "a");

fprintf(file, "%0*.0f\n", 7, 99999.00);
fprintf(file, "%0*.0f\n", 7, -99999.00);
fprintf(file, "%0*.0f\n", 7, -999.00);
fprintf(file, "%0*.0f\n", 7, 999999.00);
fprintf(file, "%0*.0f\n", 7, 9.00);

return 0;
}

C++ Builder 3 中的输出:

0099999
-0099999
-0000999
0999999
0000009

C++ Builder 5 中的输出:

0099999
-099999
-000999
0999999
0000009

您可以清楚地看到负值的 0 的填充不同。为什么是这样?这有记录吗?

提前致谢

最佳答案

我在 gcc 上试试这个:

// gcc -o test.exe test.c
#include <stdio.h>
int main(int argc, char* argv[])
{
printf("%0*.0f\n", 7, 99999.00);
printf("%0*.0f\n", 7, -99999.00);
printf("%0*.0f\n", 7, -999.00);
printf("%0*.0f\n", 7, 999999.00);
printf("%0*.0f\n", 7, 9.00);
return 0;
}

这是结果:

0099999
-099999
-000999
0999999
0000009

printf 文档说:

width : Minimum number of characters to be printed If the value to be printed is shorter than this number, the result is padded [...] The value is not truncated even if the result is larger.

他们谈论字符(减号是字符)。

我不知道 C++ 生成器。查看 printf C++ 构建器文档差异 V3/5。(这可能是 V3 中的一个错误......)

关于c - fprintf 产生不同的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24549260/

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