Thread: Simple EOF question

  1. #1
    Registered User
    Join Date
    Feb 2008
    Location
    Yokohama
    Posts
    48

    Simple EOF question

    Hi,
    I am currently studying K&R and I was looking at the example as follows:
    Code:
    #include <stdio.h>
    
    main ()
    {
        int c;
    
        c = getchar();
        while (c != EOF)
        {
            putchar(c);
            c = getchar();
        }
    }
    I am a little confused about the EOF. I checked and I am pretty sure that my EOF is -1 but when I input -1 in this program, the program doesn't end. -1 is treated just like the other characters. Am I missing something?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    Likely your program expects redirected input:
    Code:
    c:\myprog.exe < file.txt
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    40
    If you want to read until the end of input, when you are reading from stdin (i.e. if the user is typing). You want to look for end of line, or from a string (char-array) a NULL-value (0).

    If you type "-1", it will read the character - and 1, which have ascii-values which are not -1 either together or in their parts.

    Instead you could look for 10 or '\n' (that is if you expect typed input):

    Code:
        while (c != '\n')
        {
            putchar(c);
            c = getchar();
        }

  4. #4
    Registered User
    Join Date
    Feb 2008
    Location
    Yokohama
    Posts
    48
    Thanks for the replies. I am still a little confused though. I thought because EOF is defined in stdio.h as -1, that when I type in -1 the program should end.

    Your suggestions work of course and I think understand you explanation about ascii values but I wonder why nearly every example in Kernighan and Ritchie uses EOF if it doesn't work.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if it doesn't work
    it works
    EOF is not what was read from input stream - it is artifitially defined value that cannot be in any case read from input stream, and thus was selected to notify the caller that there is nothing left to read...

    to imitate the EOF condition from the keyboard you can try to use Ctrl-Z combination on Windows or Ctrl-D on *nix system...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Feb 2008
    Location
    Yokohama
    Posts
    48
    Thanks for the reply. I'm not really sure about the reasoning but I guess I'll try to just accept it as a fact.
    Why is EOF defined as -1 in stdio.h though?

    Ctrl-C worked for me. Thanks for the suggestion.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Why is EOF defined as -1 in stdio.h though?
    possible return values of fgetc are 0 - 255 (in case of success)
    closes impossible values are -1 and 256
    because errors in general are notified with negative values - I think -1 was choosen to indicate the read error
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Feb 2008
    Location
    Yokohama
    Posts
    48
    Thanks again.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Ctrl-C worked for me. Thanks for the suggestion.
    No, that just kills the program. It is not an orderly exit.

    > Why is EOF defined as -1 in stdio.h though?
    All chars are small positive integers, so it kinda made sense (I guess) to pick a value which could be never equal to a char no matter how large a char value got.

    ctrl-d or ctrl-z will work, but exactly how they work depend on your terminal driver and the I/O code in the C runtime library provided by your compiler.
    Normally you press the control key sequence at the start of a line, and sometimes you have to press return afterwards (if the input is buffered).

    If you try to press the control sequence at any other time, then some systems need you to press the same sequence again (eg. ctrl-z ctrl-z).

    Expermiment with this in a simple program to see how your specific implementation behaves.
    Code:
    if ( ch == EOF ) {
      printf( "yes, that was EOF\n" );
    }
    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.

  10. #10
    Registered User
    Join Date
    Feb 2008
    Location
    Yokohama
    Posts
    48
    Thanks for the reply and the clarification. I'm not quite sure what to do with that program yu gave me. Should I use it combination with the previous program or as a standalone. I tried it as a standalone but was told i had to declare c so I created the following program but nothing happens.

    Code:
    #include <stdio.h>
    
    main()
    {
        int ch;
    
        if ( ch == EOF )
        {
            printf( "yes, that was EOF\n" );
        }
    
    }

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Put it in a loop which prints say the decimal, hex and character value of every character received.
    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.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    All chars are small positive integers, so it kinda made sense (I guess) to pick a value which could be never equal to a char no matter how large a char value got.
    Interestingly, C99 defines EOF as a macro "which expands to an integer constant expression, with type int and a negative value, that is returned by several functions to indicate end-of-file". There is no footnote that gives the exact value required. So, EOF is typically -1 since it makes sense, but it could actually be any negative int constant?
    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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It could be any integer constant not equal to any valid character I suppose. My guess is that it started off as a literal -1, then came the EOF symbol which typically was assigned the value -1 just to avoid any surprises with any old code using a magic number.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about 'gets' and EOF
    By Sharke in forum C Programming
    Replies: 4
    Last Post: 03-11-2009, 08:41 AM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM

Tags for this Thread