“printf的'后面'scanf函数'需要pressing ENTER键两次,接受输入 [英] 'printf' followed by 'scanf' requires pressing ENTER key twice to accept input

查看:211
本文介绍了“printf的'后面'scanf函数'需要pressing ENTER键两次,接受输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C编程一个完整的初学者,我有一个问题。我会带一个简单的code为例:

I'm a complete beginner in C programming and I have a question. I'll bring a simple code as an example:

#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;
}

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

As expected, when I compile and run this program, my terminal displays the message:

"Please type the number 10."

然后等待输入,所以我键入它要求(10)的数量,和preSS <大骨节病> ENTER 。问题是,在我preSS <大骨节病> ENTER 一次,将其移动到新行,并等待更多的投入。只有在输入10和pressing一次ENTER它实际上是在(...以显示谢谢你!)移动。

Then it waits for input, so I type the number it asks for (10), and press ENTER. The problem is that after I press ENTER once, it moves to a new line and waits for more input. Only after typing 10 and pressing ENTER again does it actually move on (... to display "Thank you!").

所以看来我得preSS <大骨节病> ENTER 两次,我的输入被接受。有谁知道为什么出现这种情况?

So it appears that I have to press ENTER twice for my input to be accepted. Does anyone know why this happens?

推荐答案

实际上 scanf函数不发出提示,如果包括比格式说明其他任何事情,那么你必须非精确输入输入这种方式,这就是为什么你需要preSS <大骨节病> ENTER 试。

Actually scanf doesn't emit prompt and if you include any thing other than format specifier then you have to enter input inexactly that way that's why you are required to press ENTER again.

更改此:

scanf( "%i\n", &x );
        // ^
        // |  remove extra \n 

这样:

scanf( "%i", &x );

这篇关于“printf的'后面'scanf函数'需要pressing ENTER键两次,接受输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆