Execution skips getchar() in loop after carriage return and putchar()?

前端 未结 2 1495
长情又很酷
长情又很酷 2021-01-14 22:47

It\'s my first time posting here, so hopefully this goes as intended.

I\'m doing one of the early exercises from K&R. The objective is to use getchar() to get a

2条回答
  •  囚心锁ツ
    2021-01-14 23:02

    When you type stuff and you hit enter, everything is stored in an internal buffer. So every time the loop executes, it hits getchar who *does * go to that buffer and pulls a single character.

    But what I don't understand is why execution doesn't stop with each subsequent iteration of the loop

    The program stops only in the event where getchar doesn't find anything in that buffer. Now \n has nothing special in this case. It's just the convention that the CLI doesn't actually "send" what you type to the program until you hit return (i.e. you can hit backspace and so on).

提交回复
热议问题