Thread: whats the deal with EOF really ???

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    91

    whats the deal with EOF really ???

    hi !

    I have been trying to understand the EOF flag marker and have been unable to really figure out whats going on inside. This is a program from "K&R The C Programming Language, 2nd Edition"

    Code:
    int main()
    {
    	int c;
    
    	while ((c = getchar()) != EOF)
    		puchar(c);
    
    	return 0;
    }
    QUESTIONS :

    1. Whats the deal with author using c as an integer, "his explantion was that he needs enough space to display any type of value returned by getchar()", which I don't understand, will a "char c" also not do the exact same thing ???

    2. Whats the real deal with EOF because the above program displays the value at the first entry of "\n" so is newline regarded as the end of file marker, but the program never ends ???

    3. What values does EOF return during its working cycle and what value does it return when the flag is set ???
    ("I tried to print the EOF value and I kept getting -1 !!!" )

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    1) char c is not of adaquate size since getchar() return an integer.

    2) Instead of using the keyboard to do input try using redirection to have it read from a file. To do some simply type: [executable name] < [filename] where the stuff in the [] is the actual names .

    You could also send the EOF signal using CTRL Z in windows

    3) The exact value of EOF is system defined. However getchar() returns -1 on EOF

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    91
    why do I still get an output when its char c then if get cahr return integer.

    Shouldn't the output get reduced ????

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It would, if it was such that it wouldn't fit in the space allowed in a type char. EOF is one such item. It cannot fit in a single char. Therefore, if you insist on using one, you cannot test for EOF, because it won't fit, and will get transformed into some other value that will fit, making it no longer EOF. (So the comparison fails.)

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    A char contains all possible values of char
    getchar() returns all possible values for char AND one special value called EOF
    Therefore you need something bigger than a char to store the answer.

    > However getchar() returns -1 on EOF
    getchar() returns EOF on EOF
    The OP's current implementation prints this as -1
    YMMV.

    > Whats the real deal with EOF because the above program displays the value at the first entry of "\n"
    No, that's line buffering in action. The OS accumulates characters until you press enter. Then your program gets to run for a while, processing all the characters you entered up to and including the newline. The program is then forced to wait for the OS to accumulate another complete line.

    The program ends when you enter an EOF at the keyboard, which is usually done by pressing ctrl-z in DOS/Win32 command line or ctrl-d in Unix/Linux.
    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.

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    91
    great explnation salem and quzah, I guess K&R Programming C needs some revision in text , thanks !!!1

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Opps thanks for the catch Salem.

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by gemini_shooter
    great explnation salem and quzah, I guess K&R Programming C needs some revision in text , thanks !!!1
    What revision is needed? The quote you made in bold is true...
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

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