小编use*_*148的帖子

'printf'后跟'scanf'需要按两次ENTER键才能接受输入

我是C编程的初学者,我有一个问题.我将以一个简单的代码为例:

#include <stdio.h>

int main( void )
{
    int x;
    printf( "Please type the number 10." );
    scanf( "%i\n", &x );

    if ( x == 10 )
        printf( "Thank you!\n" );


    return 0;
}
Run Code Online (Sandbox Code Playgroud)

正如预期的那样,当我编译并运行该程序时,我的终端显示以下消息:

"Please type the number 10."
Run Code Online (Sandbox Code Playgroud)

然后它等待输入,所以我输入它要求的数字(10),然后按ENTER.问题是,在我按ENTER一次后,它会移动到一个新行并等待更多输入.只有在输入10并再次按下ENTER后才能继续(...显示"谢谢!").

所以看来我必须按ENTER两次才能接受我的输入.有谁知道为什么会这样?

c

1
推荐指数
1
解决办法
4584
查看次数

标签 统计

c ×1