Thread: Question about EOF.

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    14

    Question about EOF.

    I was reading a book about C programming, and I came across this program example:
    Code:
    #include <stdio.h>
    
    int main() {
        int c;
        
        c = getchar();
        while (c != EOF) {
              putchar(c);
              c = getchar();
        }
    }
    Ok, the author was obviously explaining getchar() and putchar() :P
    The author said that the type of c must be int, because EOF needs to fit inside too, along with all the other characters (in range from -128 to 127).

    My question is: What is EOF? What do I type to get out of the loop?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    EOF depends on your OS, shell or environment. Try either CTRL D or CTRL Z (no, no matter what any idiot that comes after me might say, it isn't CTRL C, ever).


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

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    From the CTRL D or CTRL Z Quzah mentioned, your compiler will probably give EOF a value of -1, if the file pointer is at the end of the file, or zero if it's not. CTRL Z and 0 or -1, was the standard from the DOS day.

    It's still in use by Windows.

    If you'd like to see CTRL Z in action, in Windows, open up a console (DOS) window.

    type:
    copy con Ancient.txt
    Water, water, everywhere
    And all the boards did shrink
    Water, water, everywhere
    Nor any drop to drink
    <and press CTRL z>

    You'll now have a file called Ancient.txt in your directory, with these few lines from The Rhyme of the Ancient Mariner
    Last edited by Adak; 12-05-2009 at 12:52 PM.

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    14
    Tnx, it doesn't work for CTRL D/Z, but it works for CTRL C

    One more question, but I don't want to open a new thread. The author said that you can do this:
    Code:
    #include <stdio.h>
    
    int main() {
        char c;
        
        while((c = getchar()) != 'A') printf("Lol\n");
    }
    instead of this:
    Code:
    #include <stdio.h>
    
    int main() {
        char c;
        
        c = getchar();
        while (c != 'A') {
               printf("Lol\n");
               c = getchar();
        }
    }
    But, why do both cases print "Lol\n" 2 times (in case I don't enter 'A')?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Junky View Post
    Tnx, it doesn't work for CTRL D/Z, but it works for CTRL C
    Stop! On Windows it works. I just did it (again), on Windows XP, and I've done the same thing with CTRL z with nearly every version of Windows since Windows 3.1.

    That's how I write up very small bat and text files.

    I haven't tried it yet with Windows 7.

    The posted program does the following:

    First, it fills up the keyboard buffer with your keystrokes, until you hit 'A'. Each letter is also echo'd to stdout (usually the screen).

    When the keyboard buffer is full, you will hear a beep every time you hit a key.

    If you hit enter, it prints the Lol message, once for every key stroke, which allows the getchar() to empty the keyboard buffer. In my case, that's about 256 "Lol"'s.

    If you still haven't hit 'A' key, then it waits for your next keystroke.

    But, why do both cases print "Lol\n" 2 times (in case I don't enter 'A')?
    I'm not familiar with your system or compiler.

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    14
    Windows XP, I'm working on Dev-C++.

    Thanks, I think I understand now what does it actually do. I realized that it prints "Lol\n" 1 time if I just press "Enter", 2 times if I enter 1 character, 3 times if I enter 2 characters, although I still don't understand why does it print 2 times for 1 character.
    Last edited by Junky; 12-06-2009 at 05:42 AM.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    if you mean it prints twice with every char you enter, then the answer is that the char you typed also has a char behind it, the newline, that was sent to the keyboard buffer when you hit the enter key.

    the newline is also a char (until it becomes two char's (CR & LF, when opened as a text file).

  8. #8
    Registered User
    Join Date
    Nov 2009
    Posts
    14
    Omg, you're right!!
    Tnx

  9. #9
    Registered User
    Join Date
    Nov 2009
    Posts
    14
    Ok, I'm getting really annoying with this, but I want to understand EOF completely.
    Code:
    #include <stdio.h>
    
    int main() {
        int c,nl;
        
        nl = 0;
        while ((c = getchar()) != EOF) if (c == '\n') nl++;
        printf("%d\n",nl);
    }
    This is supposed to count lines. But I can't get to the 'printf' part, because, when I enter EOF (which is CTRL C), I get kicked out of the program. Does anyone know why?
    (When I was testing it, I entered system("pause"), so that's not the problem).

  10. #10
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by Junky View Post
    Ok, I'm getting really annoying with this, but I want to understand EOF completely.
    Code:
    #include <stdio.h>
    
    int main() {
        int c,nl;
        
        nl = 0;
        while ((c = getchar()) != EOF) if (c == '\n') nl++;
        printf("%d\n",nl);
    }
    This is supposed to count lines. But I can't get to the 'printf' part, because, when I enter EOF (which is CTRL C), I get kicked out of the program. Does anyone know why?
    (When I was testing it, I entered system("pause"), so that's not the problem).
    EOF is not CTRL-C, its CTRL-Z for Windows and CTRL-D for Linux. CTRL-C is some kinda interrput(which I'm not sure).
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  11. #11
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    EOF explained
    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".
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  12. #12
    Registered User
    Join Date
    Nov 2009
    Posts
    14
    I understand now. I was confused, because CTRL-Z gave me "^Z", but I just have to press enter. Tnx

  13. #13
    Registered User
    Join Date
    Jul 2010
    Posts
    3
    I'm reading the same book on C programming as well. Though, Ctrl + Z dosen't seem to work for me. I'm using VS 2008 and windows 7 to run the code. I read somewhere that sometimes you'd have to use Ctrl + V first followed by Ctrl + Z. Is that the right way? Seems to have worked. Has anyone else tried it on windows 7?

  14. #14
    Registered User
    Join Date
    Jul 2010
    Posts
    3
    and I had to press 'Enter' after Ctrl+v AND Ctrl+z

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by dareck
    I read somewhere that sometimes you'd have to use Ctrl + V first followed by Ctrl + Z. Is that the right way? Seems to have worked. Has anyone else tried it on windows 7?
    Interesting, but that does not appear to work for me on Windows 7. I can confirm that CTRL+Z is the way to trigger EOF when writing to standard input at a command prompt for Windows. What I do is enter it on a new line.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. EOF messing up my input stream?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2005, 03:00 PM
  2. question about eof
    By ssjnamek in forum C++ Programming
    Replies: 17
    Last Post: 05-05-2005, 10:05 AM
  3. files won't stop being read!!!
    By jverkoey in forum C++ Programming
    Replies: 15
    Last Post: 04-10-2003, 05:28 AM
  4. EOF question, scanf also
    By newbie123 in forum C Programming
    Replies: 4
    Last Post: 01-31-2003, 04:15 PM
  5. what does this warningmean???
    By kreyes in forum C Programming
    Replies: 5
    Last Post: 03-04-2002, 07:53 AM