Thread: getchar() and putchar()

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    16

    getchar() and putchar()

    friends, i am new to C. I learnt today about getchar() and putchar() functions and calling them. The book (by Denis Ritchie) says that getchar reads one character at a time and putchar prints a character each time it is called. but in practice, when I input a long character string instead of just one character, it accepted and putchar printed that exact string. I am missing the point. could you please help me?

    Code:
    #include <stdio.h>
    
    main()
    {
        int c;
        c = getchar();
        while (c!=EOF)
            {
                putchar(c);
                c = getchar();
    
            }
    }
    thanks in advance..
    Last edited by kawaikx15; 04-13-2011 at 08:04 AM. Reason: adding the code

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Post your code --in Code tags please.
    None of us are mind readers.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    What do you mean by "input a long character string"? Is this part of the program reading characters from stdin? By it self putchar shouldn't print the entire string. Can you post some code where this is occurring?

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Given the code you have now posted. Putchar does in fact print one character at the time! The character just read with getchar.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    69
    First, instead of:
    Code:
    main()
    
    ---Use:
    
    int main()
    Additionally,
    function main() should be terminated with a return value:
    Code:
    int main()
    {
        ...
        return 0;
    }
    In your code, you created a loop that continuously reads one character after another.
    Code:
        while (c!=EOF)
        {
            putchar(c);
            c = getchar();
        }
    getchar() does read-in a single character.
    In a loop, as you have here, it keeps doing it, over and over.
    The same with putchar(), it keeps doing it, over and over.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I am missing the point. could you please help me?
    The point is, stdin is usually line buffered, so the standard library only returns to your program when it has a whole line. It then loops through all the available characters, before waiting once more for another whole line.

    Or you press ctrl-d (or ctrl-z) to signal EOF
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getchar putchar
    By chess2009 in forum C Programming
    Replies: 7
    Last Post: 03-06-2011, 02:44 AM
  2. getchar and putchar
    By BEN10 in forum C Programming
    Replies: 4
    Last Post: 03-11-2009, 10:29 PM
  3. Calculator using getchar, putchar
    By arlen20002000 in forum C Programming
    Replies: 5
    Last Post: 03-26-2008, 10:37 AM
  4. need help please! getchar, putchar, etc.
    By sue in forum C Programming
    Replies: 1
    Last Post: 03-21-2003, 08:40 PM

Tags for this Thread