Explaination of EOF

This is a discussion on Explaination of EOF within the C Programming forums, part of the General Programming Boards category; I was reading the FAQ, "How do I get my program to wait for a key press" If I understand ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    10

    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:
    Code:
    #include <stdio.h> 
    
    int main(void)
    {
      int ch;
      printf ("Press [Enter] to continue");
      while ((ch = getchar()) != '\n' && ch != EOF);
      return(0);
    }
    Beginner C programmer says:
    Thanks in advance

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 08: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, 03: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21