Thread: questions about getchar() and EOF

  1. #1
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534

    questions about getchar() and EOF

    Ok, I am revisiting the whole getchar() and EOF thing here.

    I am still working through K&R, as time permits (I work at a totally unrelated job, so sometimes I do not get a lot of time to learn C) and I got to exercise 1-8, where they want a program that counts blanks, tabs, and newlines. So anyway, I do not have the book with the answers to K&R. I do have some answers online, however they do not help me to understand the why of the whole thing, meaning that I don't really learn why I am doing stuff. It would be like me asking you guys to write the program for me. I think my problem lies with a poor understanding of getchar() and EOF. Of course some of you will likely tell me that my problems lie with a poor understanding of C... Anyway, if there are any of you who can help me with understanding what is going on with the question, and why my code does not do what I want it to, I would appreciate it a lot.

    Here is what I did (I will list what it does when I run it following...)

    Code:
    #include <stdio.h>
    
    /* Program to count blanks, tabs, and newlines (Exercise 1-8 in K&R) */
    
    int main(void)
    {
      int c, nl, tab, blank;
    
      nl = 0;
      tab = 0;
      blank = 0;
    
      while ( ( c = getchar() ) != EOF )
        if ( c == '\n' )
          ++nl;
      printf( "There were %d new lines\n", nl );
    
      if ( c == '\t' )
        ++tab;
      printf( "There were %d tabs\n", tab );
    
    
      if ( c == ' ' )
        ++blank;
      printf( "There were %d blanks\n", blank );
    
      return 0;
    }
    Ok, so here is what happens when I run it:

    [Tue Feb 4 ~/cprograms]$count_tabs
    testing my program. a tab here, and some spaces here

    and then a new line

    and another tab and then another, and another,

    newline

    There were 8 new lines
    There were 0 tabs
    There were 0 blanks
    It seems to do the newlines thing right, but it better, as it is basically right out of the book. Anyway, enough out of me here, as I don't get it. Hopefully someone will help. Thanks.

    kermit

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while ( ( c = getchar() ) != EOF )
    
        if ( c == '\n' )
          ++nl;
        printf( "There were %d new lines\n", nl );
    
      if ( c == '\t' )
        ++tab;
      printf( "There were %d tabs\n", tab );
    
    
      if ( c == ' ' )
        ++blank;
      printf( "There were %d blanks\n", blank );
    You need to use { } so that the code is considered the same block. See below:

    Code:
    while ( ( c = getchar() ) != EOF )
    {
        if ( c == '\n' )
            ++nl;
        printf( "There were %d new lines\n", nl );
    
        if ( c == '\t' )
            ++tab;
        printf( "There were %d tabs\n", tab );
    
        if ( c == ' ' )
            ++blank;
        printf( "There were %d blanks\n", blank );
    }
    That way all three if checks are used each time through the loop.

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

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Ok, this helps me. It 'works' a bit better now. I still have to play with it to get it to stop (I hit enter for a new line and it blasted the output onto the terminal a number of times, but from here I can carry on and see if I can make it do what I want to. I will return if I can't get it. Thanks for the tip, Quzah.

    kermit

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    From the keyboard, EOF is usually CTRL+D or CTRL+Z, OS dependant.

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

  5. #5
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Can be CTRL+C too...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getchar EOF confusion
    By webofunni in forum C Programming
    Replies: 3
    Last Post: 04-04-2009, 08:25 AM
  2. Getchar terminating with new line or EOF
    By rocksteady in forum C Programming
    Replies: 1
    Last Post: 10-19-2007, 01:19 PM
  3. while statement
    By wonderpoop in forum C Programming
    Replies: 13
    Last Post: 10-23-2006, 09:14 PM
  4. Getchar(), Keyboards, and EOF?
    By Epo in forum C Programming
    Replies: 5
    Last Post: 10-13-2004, 06:21 PM
  5. Replies: 2
    Last Post: 11-10-2003, 09:12 PM