gpt4 book ai didi

c - printf() 精度

转载 作者:太空宇宙 更新时间:2023-11-04 03:12:33 31 4
gpt4 key购买 nike

char buf[10];
int counter, x = 0;
snprintf (buf, sizeof buf , "%.100d%n", x, &counter);
printf("Counter: %d\n", counter)

我正在学习 printf 的精度。使用 %.100d%n 时,这里的精度给出了 100 个数字来渲染 x。

我不明白的是为什么计数器会增加到 100,尽管实际上只有 10 个字符被写入缓冲区?

最佳答案

写入buf的十个字节为9个空格和1个'\0'(零终止符),counter赋值100 表示 99 个空格和 1 个 '0'(数字零)。

   buf <== "         "
%.100d <== " ..... 0"

请注意 buf 是不完整的:它没有 '0'

关于c - printf() 精度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55005394/

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