i want to getchar twice but i cant

后端 未结 2 1399
梦谈多话
梦谈多话 2021-01-22 15:20
int main()
{
    int r, c;
    r = getchar();
    c = getchar();
    putchar(r);
    putchar(c);
    printf(\"\\n\");
    return(0);
}

After it reads i

相关标签:
2条回答
  • 2021-01-22 15:32

    Are you entering the characters on the same line, or on 2 lines?

    getchar() will wait until you press enter, and then start parsing the characters. If you have entered 2 characters on 2 different lines, it will read the first character and then the \n character.

    What I mean is, the following input:

    a
    b
    

    is equivalent to "a\nb".

    getchar() will grab the \n instead of b, and print a\n\n.

    You want to type both characters, and only then hit enter.

    0 讨论(0)
  • 2021-01-22 15:32

    You're probably typing X + Enter. The first getchar() reads the character X, and the second getchar() reads a newline generated when you press Enter. Type both your characters without pressing Enter.

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