what is the value of EOF?

This is a discussion on what is the value of EOF? within the C Programming forums, part of the General Programming Boards category; I checked the value of EOF and found that its value is -1.In dennis ritchie it is given that we ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    46

    what is the value of EOF?

    I checked the value of EOF and found that its value is -1.In dennis ritchie it is given that we need to take a variable which should be big enough to hold EOF value.... thats because he has taken that variable as integer type.... but why do we need integer type to store -1... why cant we declare that variable as char type??

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And if you use a char, how do you tell the difference between -1 (the character) and EOF (which has the value -1, but isn't a valid character). After all, you can use fgetc() on a binary file, which may contain ANY value between 0..255 viewed as unsigned values, or -128..127 as a signed. The C library needs to have a value for EOF that is OUTSIDE of the valid characters, right? So letting fgetc() and related functions return EOF as -1, and characters as the unsigned value 0..255 (which automatically turns into the corresponding -128..127 range when translated to signed char), then we can tell the difference between "a char with all bits set" and the error condition of "there is something wrong here", which is really what EOF signifies [but 99999 out of 100000 times you get EOF, the reason is that you've read everything from the file].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Because the ASCII charset doesn't go backwards. Reading a character of 128+ might be EOF.

    "Why can't we use unsigned char?" You ask. Same reason.

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    There is some information about EOF in the Programming FAQ.

    The way I think about it (which might not be 100% correct) is that EOF has a value, but EOF is not simply a value...

    I used to think it was exactly like the null-termination of a C-style string, but that was WRONG! If you stick a null in the middle of a string, the program will treat that null as the end of the string. If you stick a -1 in the middle of a file, the program wil not treat it as end of the file.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by DougDbug View Post
    There is some information about EOF in the Programming FAQ.
    Interesting. I think I may owe zacs7 an apology over this, tho he may not know why...



    [edit] no, it was kermit still sorry tho
    Last edited by MK27; 01-15-2009 at 08:50 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,935
    Quote Originally Posted by DougDbug
    The way I think about it (which might not be 100% correct) is that EOF has a value, but EOF is not simply a value...
    I'd say that "EOF is a value" would also be correct, since EOF is a macro.

    Quote Originally Posted by MK27
    Interesting. I think I may owe (kermit) an apology over this, tho he may not know why...
    Now I'm curious... why?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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. 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