fgetc(stdin) in a loop is producing strange behaviour

后端 未结 4 499
别跟我提以往
别跟我提以往 2020-12-11 08:24

I have this code

while(1){
    printf(\"hello world !\\n\");
    fgetc(stdin);
}

when this runs and I enter a letter like this:

<         


        
4条回答
  •  眼角桃花
    2020-12-11 09:18

    You have two characters there, 'a' and '\n'. That is the problem, because fgetc will only read ONE character. This is the documentation.

    If you enter only a '\n' - hit enter only - you will have the expected behaviour.

    Hope it helps!

提交回复
热议问题