Thread: Explaination of EOF

  1. #1
    Registered User
    Join Date
    Oct 2003

    Explaination of EOF

    I was reading the FAQ, "How do I get my program to wait for a key press"
    If I understand the example correctly, (and please correct me if I'm wrong) the program gets input until it recives a newline when it then returns 0, but what about the section of code that says "ch != EOF'"

    heres the example:
    #include <stdio.h> 
    int main(void)
      int ch;
      printf ("Press [Enter] to continue");
      while ((ch = getchar()) != '\n' && ch != EOF);
    Beginner C programmer says:
    Thanks in advance

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    EOF is End Of File

    Like you would get if you did
    myprog < some_file_with_no_newlines

    But if you're just using the normal stdin (the keyboard), you generate an EOF by pressing either ctrl-z (DOS/Win) or ctrl-d (Unix/Linux)

    EOF is the only value which is guaranteed to show up at some point, so you must check for it.
    Otherwise, your loop could become an infinite loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. EOF Explanation Anybody?
    By blackcell in forum C Programming
    Replies: 1
    Last Post: 01-29-2008, 09:09 PM
  2. EOF or not EOF?
    By CornedBee in forum Linux Programming
    Replies: 2
    Last Post: 09-14-2007, 02:25 PM
  3. EOF messing up my input stream?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2005, 03:00 PM
  4. whats the deal with EOF really ???
    By gemini_shooter in forum C Programming
    Replies: 7
    Last Post: 03-06-2005, 04:04 PM
  5. files won't stop being read!!!
    By jverkoey in forum C++ Programming
    Replies: 15
    Last Post: 04-10-2003, 05:28 AM