Thread: getchar()

  1. #16
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I don't like using escape characters because most normal users don't know about them.

    Sigh, I apoligize. In the face of evidence I can't refute I must say I was wrong. I do believe the code is not the best choice for standard input but I do concede it is valid.

  2. #17
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Well, to toss further logs on the fire, I think Thantos is correct in this case, although he does have misteaks in his posts.
    1) Don't use EOF. Test it against '\n'
    Correct. The end of your input line is '\n', as defined by getchar();
    2) The process won't even go into the loop until the Enter key is pressed. Once it the enter key is pressed then the loops starts and getchar() gets the letters from the buffer for the input stream.
    Correct again
    3) You are saying c is an integer but you are assigning it a character. See the problem here?
    incorrect. As pointed out, getchar() does in fact return an int
    4) Only way to get the effect you want is to go a different route.
    Also incorrect. quzah's answer about using fflush(stdout); will in fact give the correct output
    AFAIK, the only way to get the EOF character is to physically type ^Z, which is silly to program for. I used to use this all the time before the PC came around, but other ways have been developed since that work better.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #18
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    Well it's entirely what the author of this code wants. In this case, I think the author coming from a unix background and used to entering EOF and piping stdin would want this behavior. But I have seen other people get stuck on this piece of code from his book so perhaps he should have made note of it. Another book that uses similar input style is Dietel's C++ book and they make note of it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getchar() problem
    By jlharrison in forum C Programming
    Replies: 6
    Last Post: 01-25-2006, 02:49 PM
  2. getchar buffer size
    By oncemyway in forum C Programming
    Replies: 3
    Last Post: 08-02-2005, 12:49 AM
  3. getchar() problem from K&R book
    By anemicrose in forum C Programming
    Replies: 13
    Last Post: 04-04-2004, 11:06 PM
  4. help with getchar lol
    By Taco Grande in forum C Programming
    Replies: 5
    Last Post: 03-18-2003, 09:25 PM
  5. Can anybody take a look at this?
    By TerryBogard in forum C Programming
    Replies: 10
    Last Post: 11-21-2002, 01:11 PM