Problems with scanf

前端 未结 4 1419
粉色の甜心
粉色の甜心 2020-12-20 09:05
#include 

int main()
{

    char C, B;
    int x;



    printf(\"What comes after G\\n\");
    scanf(\"%c\", &C);

    printf(\"What comes after         


        
4条回答
  •  萌比男神i
    2020-12-20 09:35

    The reason for this problem is newline character \n leftover by the previous scanf after pressing Enter. This \n is left for the next call of scanf.
    To avoid this problem you need to place a space before %c specifier in your scanf.

    scanf(" %c", &C);  
    ...
    scanf(" %c", &B);
    ...
    scanf(" %c", &X);  
    

    A space before %c is able to eat up any number of newline characters.

提交回复
热议问题