getchar function

This is a discussion on getchar function within the C Programming forums, part of the General Programming Boards category; hi All, I am running the following C program. Code: #include <stdio.h> main() { int c; c = getchar(); while(c ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    4

    getchar function

    hi All,

    I am running the following C program.

    Code:
    #include <stdio.h>
    main()
    {
            int c;
            c = getchar();
            while(c != 97)
            {
                    putchar(c);
                    c = getchar();
            }
    }
    I get foll. output if I enter 'qweptg' followed by enter key as input:

    qweptg
    qweptg

    I dont understand why I am getting repeated output.
    Is it something related to buffering?
    Please help me in understanding this.
    Thanks
    Nitin

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    Are you sure the first one isn't your input?

    EDIT: Also, I don't see an 'a' in your input (ASCII value 97) so how do you expect your loop to stop? It's clearer to use 'a' instead of 97 by the way.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    4

    getchar function

    the first line i get as soon as i type the 'qweptg' characters,
    as getchar function reads a character and the putchar function
    immediately sends it to std. output i.e. monitor.
    when i press enter key after these, i get the second line
    of output. After this i use ctrl C to exit the program.
    Why the second line is displayed i wonder.
    I agree using 'a' would make code more readable
    than using 97. But my main question is about why the
    characters are displayed twice?

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    4
    Also I forgot to mention -
    while loop contains a call to getchar function,
    so loop stops there until i type some character.
    and it need not be 'a'.

  5. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    itsme86 is right. It's your input. You don't getchar() until after you hit Enter.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    putchar function immediately sends it to std. output
    I think your misunderstanding is here. By default, most terminals are in a "buffered" mode. Which means that your input is queued up until you press ENTER or the program ends or a few other circumstances happen. What you're seeing when you press a key is an ECHO of your input. Most terminals do this by default. It's not your putchar() that's putting it there on the first line.

    Or maybe what you're saying is that you were expecting the output to be: qqwweeppttgg

    That would only happen if the terminal was in "unbuffered" mode. What's really happening in your program is that it's sitting at the very first getchar() while you're typing your input until you press ENTER. Then the loop runs and picks up each character on the input queue and putchar()s them one at a time.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    But my main question is about why the
    characters are displayed twice?
    Because the first time, you type them. And then you print them again with putchar().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Jul 2005
    Posts
    4
    oh ok.
    that clears up most of my doubts.
    I think i misunderstood the buffering, getchar
    concepts.
    so whatever keys i type are kept by
    the terminal in buffer and also are displayed,
    which accounts for the first line of output.
    The second line is the actual result of the
    while loop processing.

    Thanks guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21