Simple EOF question

This is a discussion on Simple EOF question within the C Programming forums, part of the General Programming Boards category; Hi, I am currently studying K&R and I was looking at the example as follows: Code: #include <stdio.h> main () ...

  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,006
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    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...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    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?
    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

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 08:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 11:15 AM
  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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21