gpt4 book ai didi

c - 为什么 scanf 卡在 while 循环中?

转载 作者:太空狗 更新时间:2023-10-29 15:20:01 26 4
gpt4 key购买 nike

我在 while 循环中使用了 scanf()。

while(1)
{
int input = 0;
scanf("%d\n", &input);
printf("%d\n", input);
}

当我运行这个程序并输入一个数字时,除非我再次输入另一个数字,否则 printf() 不会显示该数字。为什么?

最佳答案

scanf 格式中的 \n 被解释为“任意数量的空白”。它会不断读取空格(空格、制表符或回车符),直到遇到不是空格的内容。

关于c - 为什么 scanf 卡在 while 循环中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17518290/

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