Thread: EOF

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    3

    EOF

    So I tried searching google a bit for what exactly EOF is in C and what it does, and I keep getting these complex explanations for it. I'm just starting out in C, so could someone dumb it down for me and explain what EOF is and does?

    this is the code. it's taken from page 18 of K&R's The C Programming Language.

    Code:
    #include <stdio.h>
    main()
    {
                double nc;
                for (nc = 0; getchar() != eof; ++NC);
                printf(%.0f\n", nc);
    }

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    eof is not the same as EOF, which is most certainly what was in K&R (Remember that things are case sensitive in the C environment). EOF stands for End Of File. Your operating system keeps track of file sizes and so on, so when there are no more bits in a file to be read, you have an EOF, or end of file. It is good to know when you have reached end of file, and so you can test for it with the EOF macro. In the case of testing for EOF interactively (such as your program, where the user is entering the input) you would typically do a ctrl-z key combination on a Windows machine, or ctrl-d on a Unix type machine to indicate EOF. Have a look at these FAQ entries, for a little more info about EOF:



    As for your particular program, the variable nc is to hold the amount of characters obtained from the keyboard. getchar() gets a character at a time, until EOF is indicated. Every time through the loop, a character is obtained (by getchar()) and the variable nc is incremented by one. By the way, just as eof is not the same as EOF, nc is not the same as NC.
    Last edited by kermit; 08-02-2010 at 03:10 PM.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    getchar() basically returns a number. This number might represent a character (for instance, ASCII characters), or it might be some status code returns by the function. EOF is on of those codes - it isn't a true character, but it's a signal that the function reached the end of the file.

  4. #4
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    EOF stands for end-of-file. It's value is -1.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Babkockdood
    EOF stands for end-of-file. It's value is -1.
    You might want to read the existing replies instead of repeating the information. Furthermore, it is not guaranteed that EOF is -1. It is only guaranteed that EOF is a negative value of type int.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    53
    Quote Originally Posted by laserlight View Post
    You might want to read the existing replies instead of repeating the information. Furthermore, it is not guaranteed that EOF is -1. It is only guaranteed that EOF is a negative value of type int.
    With reference to the below link

    [html]http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1048865140&id=1043284351[/html]

    "EOF is a macro defined as an int with a negative value." -> If it is a macro, then there should be some value defined right? How can it be sometimes -1 and sometimes other?

  7. #7
    Novice
    Join Date
    Jul 2009
    Posts
    568
    What's it defined as depends on the implementation. It can be one negative value of type int in MSVC compiler and another in GCC.

    You can't count on it being -1.

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    53
    Quote Originally Posted by msh View Post
    What's it defined as depends on the implementation. It can be one negative value of type int in MSVC compiler and another in GCC.

    You can't count on it being -1.

    Thanks

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    53
    If EOF is defined as -1, is the below code correct?

    Code:
    signed char ch;
    FILE *fp;
    --------
    --------
    if( (ch = getchar(fp)) != EOF)
    {
    }
    My understanding: If say fp has reahced EOF, as it is an signed int the value of getchar(fp) returns 0xFFFF. Then it will be demoted to char as 0xFF in ch. And while comparing with EOF, it will be promoted to int with the value 0xFFFF whose decimal value is -1. So can we use signed char variable to hold the return value of getchar() in this case?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    By asking that question, you're missing the point. No, that code is not correct, because EOF might not be defined as -1. ch should be an int, not a signed char.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Jan 2009
    Posts
    53
    Quote Originally Posted by laserlight View Post
    By asking that question, you're missing the point. No, that code is not correct, because EOF might not be defined as -1. ch should be an int, not a signed char.
    you are also mentioning that "might not be defined"... I too know that it may not be exactly -1 always, it differs from compiler to compiler. My query is if say it is defined as -1

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by karthik537
    you are also mentioning that "might not be defined"... I too know that it may not be exactly -1 always, it differs from compiler to compiler. My query is if say it is defined as -1
    Fine: yes, you are right to say that having ch as a signed char could be acceptable in this case. That said, there is still the possibility of an implementation defined conversion should a character returned by getchar() have a value outside of the range of signed char (such characters are obtained as unsigned char converted to int).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Jan 2009
    Posts
    53
    Quote Originally Posted by laserlight View Post
    Fine: yes, you are right to say that having ch as a signed char could be acceptable in this case. That said, there is still the possibility of an implementation defined conversion should a character returned by getchar() have a value outside of the range of signed char (such characters are obtained as unsigned char converted to int).
    you are right! let us say if the character read by getchar() is ' '(space) whose value is 255, then obviously as it is the out of the range of signed char, it will be obtained as 255 converted to int....

  14. #14
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    Quote Originally Posted by karthik537 View Post
    you are right! let us say if the character read by getchar() is ' '(space) whose value is 255, then obviously as it is the out of the range of signed char, it will be obtained as 255 converted to int....
    In your hypothetical implementation that you are proposing, what are the values in <limits.h> of:

    CHAR_BIT
    SCHAR_MIN
    SCHAR_MAX
    UCHAR_MAX

    Is (plain) char signed or unsigned?

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by pheininger
    In your hypothetical implementation that you are proposing, what are the values in <limits.h> of:
    I do not think those matter since 255 is just a hypothetical value. One can always find some value in the range of unsigned char that is not in the range of signed char.

    Quote Originally Posted by pheininger
    Is (plain) char signed or unsigned?
    That does not matter since that type is not involved here.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. while ((c = getchar()) != EOF), AND cntrl z
    By Roger in forum C Programming
    Replies: 8
    Last Post: 10-21-2009, 09:25 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