Thread: Eof

  1. #1
    Ian2
    Guest

    Eof

    i was reading the FAQ here and in the the c implementation for waiting for a key press the code is:

    #include <stdio.h>

    int main(void)
    {
    int ch;
    printf ("Press [Enter] to continue");
    while ((ch = getchar()) != '\n' && ch != EOF);
    return(0);
    }

    ...what is EOF???
    TY

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    76
    EOF is a constant defined as -1. It also indicates the end of a file when using the file-reading function fread and the like.

    getchar returns EOF when the end of stdin has been reached and the user has inputted no more characters.

  3. #3
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Just a warning, all that the standard (7.19.1) requires is that
    EOF ... expands to an integer constant expression, with type int and a negative value, that is returned by several functions to indicate end-of-file, that is, no more input from a stream
    So, it is possible in theory to have an implementation where the value of EOF is not -1. So, it would not be a good idea to replace EOF by -1 .
    The one who says it cannot be done should never interrupt the one who is doing it.

  4. #4
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    So, it would not be a good idea to replace EOF by -1 .
    It's nothing wrong if you replace it with -1. As poccil already wrote, EOF is a constant.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Yes, EOF is a constant, but I agree with pinko_liberal, since the standard says that the value of the EOF constant is not per definition -1. So it would be recommendable to use EOF instead of -1. If you would compile on a different system, which uses a different value for EOF, then you might get problems.

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