c - 输出自 Action 为C中的输入

标签 c

这是简单的代码。我正在接受输入,打印它,然后再次接受并再次打印。

第二次不让我输入。

谁能解释一下为什么?

#include "stdio.h"
int main()
{
char buff[50];

printf("Enter a String\n");
scanf("%[^\n]s",buff);

puts(buff);

printf("Enter another String\n");
scanf("%[^\n]s",buff);

puts(buff);

return 0;
}

输出:

Enter a String
Hello                  // This I have entered
Hello                  // This is the output
Enter another String
Hello                  // This I haven't entered, It automatically takes this.

最佳答案

在第二个 scanf() 中,在输入说明符的开头放置一个空格:

scanf(" %[^\n]s",buff);
      ^^^

这将吃掉当前第二次满足 scanf 的字符,并允许您输入第二个字符串。

关于c - 输出自 Action 为C中的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29779192/

相关文章:

c - 纯头文件 ANSI C 库如何可能?

c - 如何在 C 中将 unsigned char 打印为 2 位十六进制值?

c - 为什么数组接收的值可以超过它声明的值

c++ - 用于计算位或找到最右边|最左边的位的高效按位运算

c - 如何动态计算动态分配内存的大小

c - iOS 7 - 在 C 中获取唯一 ID

CUDA 没有按预期工作?

C 嵌入式系统堆栈和堆大小

c++ - 将一些代码行包​​装到单个宏中的方法

python - OpenCV 点目标检测未找到所有目标,发现圆圈偏移