gpt4 book ai didi

c - 使用 %3d(scanf() 中的最大字段宽度说明符)时出现意外输出

转载 作者:行者123 更新时间:2023-11-30 14:54:40 25 4
gpt4 key购买 nike

源代码:

#include <stdio.h>
main()
{
int a,b,c;
printf("Enter: ");
scanf("%3d %3d %3d",&a,&b,&c);
printf("%d",b);
}

输出:

Enter: 1234 5678 9
4
--------------------------------
Process exited after 7.322 seconds with return value 1
Press any key to continue . . .

当我在这里使用 scanf("%3d %3d %3d",&a,&b,&c); 时,为什么 b 得到的值是 4 而不是 456?

使用 %3d %3d %3d 应提取(三次)三个连续的非空白字符并将它们放入变量 abc 分别,不是吗?

最佳答案

正如 @SouravGosh 指出的 %nd,其中 n 是数字格式,指定作为该字段的一部分读取的最大字符数。

所以输入“1234␣567␣8”,a消耗123; b 消耗了 4 并且因为␣而停止;然后c消耗了567。

关于c - 使用 %3d(scanf() 中的最大字段宽度说明符)时出现意外输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46571743/

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