Like Tree1Likes
  • 1 Post By AndrewHunter

K&R book example for »getchar()« program stumps me

This is a discussion on K&R book example for »getchar()« program stumps me within the C Programming forums, part of the General Programming Boards category; I must say that I'm just starting with C Programming. I've had the urge to do so and decided to. ...

  1. #1
    Registered User ChipOManiac's Avatar
    Join Date
    Sep 2011
    Location
    Talpittiya, Shri Langka
    Posts
    6

    Question K&R book example for »getchar()« program stumps me

    I must say that I'm just starting with C Programming. I've had the urge to do so and decided to. Prior to this however, I was a complete BASIC programmer, which makes it a little hard for me to understand some things.

    K&R's »The C Progamming Language« is the book that I have decided to use (I've tried Bruce Eckel's »Thinking In C« before, but nevertheless thought this might clear some problems)

    Chapter 1.5.4 »Counting Words« has a program that I found hard to understand:
    Code:
    #define IN 1
    #define OUT 0
    
    main()
    {
      int c, nl, nw, nc, state;
      
      state = OUT;
      
      nl = nw = nc = 0;
      
      while ((c = getchar()) != EOF) 
      {
        ++nc;
        if (c == '\n')
        {
          ++nl;
        }
        
        if (c == ' ' || c == '\n' || c == '\t')
        {
          state = OUT;
        }else if (state == OUT) {
          state = IN;
          ++nw;
        }
      }
      printf("%d %d %d\n", nl, nw, nc);
    }
    I cannot understand how this works. How does the while loop allow the number of words to be counted? I don't see c having any index operations on it.

    Bashing is accepted humbly

    Thank you!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,762
    Well have you figured out that nl = number of lines, nw = number of words and nc = number of characters.
    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.

  3. #3
    Registered User ChipOManiac's Avatar
    Join Date
    Sep 2011
    Location
    Talpittiya, Shri Langka
    Posts
    6

    Well Yes

    Yes, I was expecting an answer like this. But how are these variables supposed to help. Shouldn't there be an index or something to tell the position of the letter in order to Move forward?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,640
    Quote Originally Posted by ChipOManiac View Post
    Yes, I was expecting an answer like this. But how are these variables supposed to help. Shouldn't there be an index or something to tell the position of the letter in order to Move forward?
    There is no position, getchar just reads input from the keyboard, which is a stream. To count words, you only need to know when you cross a boundary, from non-word (state == OUT) to word (state == IN). Note that this program considers space, newline and tab "non-word" characters, everything else would be considered a "word" character.
    Quote Originally Posted by ChipOManiac View Post
    Bashing is accepted humbly
    BASIC?!?!?
    Quote Originally Posted by Edgser W Dijkstra
    It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    10
    Do you understand what happens when you use this code?
    Code:
    while ((c = getchar()) != EOF) 
    {
    ...
    }
    Spend a day or two until you have a real understanding of this and you will be able to finish the chapter much easier. Once you do, you will know what the exercises are about and what the implementation need to look like to be any good.

  6. #6
    Registered User ChipOManiac's Avatar
    Join Date
    Sep 2011
    Location
    Talpittiya, Shri Langka
    Posts
    6

    Oh Shucks!

    Quote Originally Posted by anduril462 View Post
    There is no position, getchar just reads input from the keyboard, which is a stream. To count words, you only need to know when you cross a boundary, from non-word (state == OUT) to word (state == IN). Note that this program considers space, newline and tab "non-word" characters, everything else would be considered a "word" character.
    Does that mean that at every while loop the whole string is sent character by character to the body?

    Quote Originally Posted by anduril462 View Post
    BASIC?!?!?
    Oh shucks! I knew I should have started with C Am I really doomed though?

  7. #7
    Registered User ChipOManiac's Avatar
    Join Date
    Sep 2011
    Location
    Talpittiya, Shri Langka
    Posts
    6
    Quote Originally Posted by Newbi2C View Post
    Do you understand what happens when you use this code?
    Code:
    while ((c = getchar()) != EOF) 
    {
    ...
    }
    Spend a day or two until you have a real understanding of this and you will be able to finish the chapter much easier. Once you do, you will know what the exercises are about and what the implementation need to look like to be any good.
    Thank you! apparently this is probably all I need.

  8. #8
    Registered User
    Join Date
    Sep 2011
    Posts
    10
    Quote Originally Posted by ChipOManiac View Post
    Thank you! apparently this is probably all I need.
    If you understand what the loop does, you will have no problem interpreting the conditional expressions used for the program. What the program does is basically to take characters and place them in c until the EOF (end of file) is reached if the character is a space, tab or newline (return) it goes into a new state where it is looking for a character that is not a space, tab or newline. When/if it run into one; it add 1 to nw (new word).

    Since the state machine is the same in both BASIC and C I assumed you had some problem understanding what the while-loop did. I think Q-BASIC (which is the first BASIC I tried) has the same code once within the IF-conditional(?*)

    *English is not my 1:st language...

  9. #9
    Registered User ChipOManiac's Avatar
    Join Date
    Sep 2011
    Location
    Talpittiya, Shri Langka
    Posts
    6

    Thank You Newbi2C

    Quote Originally Posted by Newbi2C View Post
    Since the state machine is the same in both BASIC and C I assumed you had some problem understanding what the while-loop did. I think Q-BASIC (which is the first BASIC I tried) has the same code once within the IF-conditional(?*)
    I was under the assumption that the while loop was waiting for an EOF to break the process of line-by-line input, not that it was using getchar() to feed each character from the stream.

    I thought that, since I had used fgets previously and that getchar seemed to just push out a string that, getchar was just the same as fgets. Which brought me to the »index« issue (gosh I'm ignorant)

    There's no excuse for it, I apologize.

    However thanks for »not fishing for me«

    Thanks to the others as well.

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Since you are working through K&R, you may find some value in Steve Summit's Notes to Accompany K&R
    BIOS likes this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  11. #11
    Registered User ChipOManiac's Avatar
    Join Date
    Sep 2011
    Location
    Talpittiya, Shri Langka
    Posts
    6
    Quote Originally Posted by AndrewHunter View Post
    Since you are working through K&R, you may find some value in Steve Summit's Notes to Accompany K&R
    Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getchar() is ignored by my program
    By stavos77 in forum C Programming
    Replies: 6
    Last Post: 03-11-2010, 03:25 PM
  2. I need help with this program its from a C++ book
    By dark21 in forum C++ Programming
    Replies: 7
    Last Post: 03-27-2009, 04:30 AM
  3. Using getchar to keep program running
    By shadow1515 in forum C Programming
    Replies: 8
    Last Post: 05-25-2008, 03:33 AM
  4. getchar() problem from K&R book
    By anemicrose in forum C Programming
    Replies: 13
    Last Post: 04-05-2004, 12:06 AM
  5. my program skips scanf and getchar()
    By jk81 in forum C Programming
    Replies: 15
    Last Post: 11-29-2002, 05:54 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