c - 我在调用 scanf() 时输入 Ctrl - D (EOF),然后忽略下一个 scanf

标签 c scanf stdin eof

我在调用 scanf() 时输入 Ctrl - D (EOF),然后忽略下一个 scanf。

#include <stdio.h>

int main()
{
   int input;
   scanf("%d", &input);//I press Ctrl-D when this line
   scanf("%d", &input);//this line just passed. not read my input. why?
   return 0;

}

我想通过第二次调用 scanf() 来获取输入。 有什么问题吗?

最佳答案

看来你的代码没问题。唯一的想法是记住在两个输入数字的末尾按“返回”。我以“更好”的方式重写您的代码:

#include <stdio.h>
int main(){

int input1,input2;
printf("Digit the first number: ");
scanf("%i", &input1);//Press return at the end
printf("Digit the second number: ");
scanf("%i", &input2);//Press return at the end
printf("input1:%i\tinput2:%i\n",input1,input2);
return 0;
}

关于c - 我在调用 scanf() 时输入 Ctrl - D (EOF),然后忽略下一个 scanf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44175629/

相关文章:

c - 如何为具有 SRC、OBJ 和 BIN 子目录的 C 项目创建 Makefile?

c - 避免在 makefile 中重复行

c - fgets 设置为 char * 导致段错误,动态字符串

c - 不兼容的指针不允许将 csv 放置到二维数组中

php - 如何使用 php 处理来自 C 程序的输入

c - 是否需要初始化 char 数组以获得准确的长度?

c - 简单的C数组问题

c++ - 使用 cin 跳过预期的字符,如 scanf()

python - 读取流并传递给子进程

Perl:测试输入阅读器?