'printf' followed by 'scanf' requires pressing ENTER key twice to accept input

前端 未结 2 1342
你的背包
你的背包 2021-01-25 18:35

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

#include 

int main( void )
{
    int x         


        
相关标签:
2条回答
  • 2021-01-25 19:06

    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.

    Change this :

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

    to this:

    scanf( "%i", &x );
    
    0 讨论(0)
  • 2021-01-25 19:09

    It's the "\n" in your call to scanf. Remove that and scanf will return after scanning the integer value you requested.

    Note: This has nothing to do with the fact that the scanf call is preceded by a printf.

    0 讨论(0)
提交回复
热议问题