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

前端 未结 2 1498
长情又很酷
长情又很酷 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 22:54

    while (c != EOF) ends after hitting EOF. Try pressing Ctrl + D after some input.

    0 讨论(0)
  • 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).

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