c - scanf 接受 int 但仅在输入另一个 char/int 并按 enter 后才继续执行

标签 c scanf

在我的代码中(在 c 中)scanf() 取一个整数值,但是当我键入一个整数并按回车键时,什么也没有发生(执行没有按预期继续进行)。我需要按下一个键,然后程序才能继续使用我首先按下的数字。

例子: 代码:

int num=0;
printf("Enter a number and see if he belong to one of the groups:");
scanf("%d\n\n",&num);

输出:

Enter a number and see if he belong to one of the groups:5(enter) 

f(enter)

然后代码才继续......

最佳答案

好吧,看来你是在告诉 scanf 读取两个换行符,而不是一个:

scanf("%d\n\n", &num);

这应该给出正确的行为:

scanf("%d\n", &num);

关于c - scanf 接受 int 但仅在输入另一个 char/int 并按 enter 后才继续执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20502488/

相关文章:

c - 错误 C4996 : 'scanf' : This function or variable may be unsafe in c programming

c - C 中 scanf 的奇怪行为

C scanf 循环中输入无效

c++ - 解码 JPEG 以获得未压缩的数据

C-返回字符串

c - Scanf 用于多个用户输入

c - scanf 将 "junk"值赋给变量

c - 尝试在 C 中调用自定义函数,但 scanf() 导致出现问题

c++ - 如何正确扩展这个宏?

c - 同类指针的区别