C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-03-2008, 09:23 PM   #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?
deadhippo is offline   Reply With Quote
Old 05-03-2008, 09:36 PM   #2
Just Lurking
 
Dave_Sinkula's Avatar
 
Join Date: Oct 2002
Posts: 4,990
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.*
Dave_Sinkula is offline   Reply With Quote
Old 05-03-2008, 09:43 PM   #3
Registered User
 
Join Date: Apr 2008
Posts: 31
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();
    }
Zarniwoop is offline   Reply With Quote
Old 05-03-2008, 10:35 PM   #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.
deadhippo is offline   Reply With Quote
Old 05-03-2008, 10:42 PM   #5
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,242
Quote:
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...
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 05-03-2008, 10:46 PM   #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.
deadhippo is offline   Reply With Quote
Old 05-03-2008, 10:52 PM   #7
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,242
Quote:
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
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 05-03-2008, 10:53 PM   #8
Registered User
 
Join Date: Feb 2008
Location: Yokohama
Posts: 48
Thanks again.
deadhippo is offline   Reply With Quote
Old 05-04-2008, 01:29 AM   #9
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
> 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.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 05-04-2008, 01:37 AM   #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" );
    }

}
deadhippo is offline   Reply With Quote
Old 05-04-2008, 01:40 AM   #11
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
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.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 05-04-2008, 03:16 AM   #12
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,364
Quote:
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
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 05-04-2008, 05:16 AM   #13
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
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.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Reply

Tags
eof

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 06:08 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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