i want to getchar twice but i cant

后端 未结 2 1428
梦谈多话
梦谈多话 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.

提交回复
热议问题