-3

how will my code behave if I do

while ((ch = getchar()) != EOF)

OR

while ((ch = getchar()) != '\n')

I am currently using C language?

  • 1
    Try it and see. – Cheatah Aug 10 '21 at 01:43
  • 2
    One will read characters until the end of a line, the other will read until the end of the file, just as the code implies. – Jonathon Reinhart Aug 10 '21 at 01:43
  • There's a newline at the end of every line. There's an EOF only after the last line. – Barmar Aug 10 '21 at 01:45
  • @Barmar: There is no guarantee there is a new-line character at the end of every line. If the user presses control-D twice with the default terminal settings on Unix settings, the currently typed characters are sent with no new-line character, and then an EOF is indicated. If input comes from a file on disk with no terminal new-line character, there is none when read. If input comes from a pipe and the other side terminates without flushing its buffers, characters may have been sent in the pipe with no new-line character. Code should not be written to assume new-lines are present. – Eric Postpischil Aug 10 '21 at 02:13
  • ch needs to be declared int, not char. – stark Aug 10 '21 at 11:38

1 Answers1

0

EOF means End of File.

\n means "newline".

  • 1
    Could you elaborate? – John Kugelman Aug 10 '21 at 01:49
  • 1
    Are you really asking me to elaborate on the difference between the difference between the terms "end of file" and "newline"? –  Aug 10 '21 at 01:50
  • 1
    I was hoping you could explain further to better help the asker. They don't understand the difference is and I think a longer answer would help them. I'm sorry to upset you. I didn't mean to. – John Kugelman Aug 10 '21 at 02:09