Thread: Beginner C - Trouble with getchar and EOF

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    4

    Beginner C - Trouble with getchar and EOF

    Hello everyone, I'm new to C and want to learn more about it. My main learning source is the (ANSI) C Programming Language 2nd ed. book by Brian Kernighan and Dennis Ritchie. As for previous programming experiences, I've dabbled a bit in Visual Basic and Maple.

    My main issue began when I started working on the chapter that deal with getchar() and EOF parameters. The book listed several example code that use getchar() to read and count the number of characters, lines, and words from input.

    So I began using these codes and compiled it with Visual Studio 2008 Command Prompt (under Windows 7) without any syntactical issues. However, when I open my .exe and test it, I was surprise that no matter how many times I enter in any letters or numbers, the program did not count and print the expected value. I've double checked everything, copy and paste codes exactly, went on several different sites and test different versions of codes, run program under winxp, nothing have yet to provide my desired result. Here is an example of one variation which counts the number of characters from an input:

    Code:
    #include <stdio.h>
    
    void main()
    {
        int c, nc = 0;
    
        while ( (c = getchar()) != EOF ) nc++;
    
        printf("Number of characters in file = %d\n", nc);
    }
    So either I am confused about what these codes suppose to do or there is some special command that I am missing. I really hope someone can help me. I am beginning to love C and don't want to be stuck
    Last edited by namath91; 05-11-2011 at 07:32 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You do realize this code will count things like spaces, enter-key, invisible control characters, etc?

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    4
    Quote Originally Posted by tabstop View Post
    You do realize this code will count things like spaces, enter-key, invisible control characters, etc?
    Yes I figured that, but what I am confused with is how to get the program to reach EOF and deliver the count of characters/words/lines.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you're typing it yourself (as opposed to redirecting input from a file), you have to type the EOF character at the keyboard. Under Windows 7, that means "type Ctrl-Z as the first and only thing on a line and press enter".

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    4
    Quote Originally Posted by tabstop View Post
    If you're typing it yourself (as opposed to redirecting input from a file), you have to type the EOF character at the keyboard. Under Windows 7, that means "type Ctrl-Z as the first and only thing on a line and press enter".
    Ah yes I forgot to mention that this is for typing input directly and not referring to external file. Thank you very much though, you've solved my problem. Is Ctrl-Z the EOF command for all windows version or just 7? I really wish my book or the sites I've been to mention it...

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    4
    Quote Originally Posted by quzah View Post
    Thanks, I'll remember to check wiki next time.

    Ironically when I went to cprogramming.com faq page:
    Cprogramming.com FAQ > Definition of EOF and how to use it effectively

    I was overwhelmed and couldn't find any solution to my problem.

    I love this forum already

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by namath91 View Post
    I was overwhelmed and couldn't find any solution to my problem.

    I love this forum already
    That's because you went to an FAQ about the C programming language. "How do I run my computer" is not a question whose answer is going to appear in that sort of an FAQ.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by namath91 View Post
    Yes I figured that, but what I am confused with is how to get the program to reach EOF and deliver the count of characters/words/lines.
    Are you by chance clicking it with the mouse?

    Open a CMD shell... and run it there.

    It's probably closing so fast you don't see the result.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. // Trouble launching 2nd thread from MainWindow, beginner //
    By RickTrelles in forum Windows Programming
    Replies: 2
    Last Post: 07-03-2008, 09:43 AM
  2. Replies: 6
    Last Post: 01-03-2007, 03:02 PM
  3. Beginner in Trouble!
    By Sarina in forum C++ Programming
    Replies: 4
    Last Post: 10-23-2004, 02:35 PM
  4. A beginner in trouble
    By Eldey in forum C++ Programming
    Replies: 3
    Last Post: 09-13-2004, 09:16 AM
  5. trouble with a loop (beginner)
    By Procta in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2003, 10:27 AM