Thread: Working of getchar() and putchar().

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    3

    Working of getchar() and putchar().

    Code:
    #include <stdio.h>
       /* copy input to output; 2nd version  */
    main()
       {
       int c;
       while ((c = getchar()) != EOF)
          putchar(c);
       }
    (As described in the book)The while gets a character, assigns it to c, and then tests whether the character was the end-of-file signal. If it was not, the body of the while is executed, printing the character. The while then repeats. When the end of the input is finally reached, the while terminates and so does main.

    I am learning C from book C programming by Ritchie. Now when i am running this program, i am not getting the same output as its explained in the book.
    Please help.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You probably need to enter CTRL + D or CTRL + Z on a new line to trigger the EOF condition.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    3
    Thanks a lot @laserlight. Its working with Ctrl+M. There is one more problem that Getchar(), Or putchar(), should return or display only a character. But i am entering "Hello how are you?" and when i press Enter it displays the whole line. But it should display character wise.

  4. #4
    Registered User
    Join Date
    May 2013
    Posts
    3
    Thanks a lot @laserlight. Its working with Ctrl+M. There is one more problem that Getchar(), Or putchar(), should return or display only a character. But i am entering "Hello how are you?" and when i press Enter it displays the whole line. But it should display character wise.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Its working with Ctrl+M
    That's the same as pressing the enter key.

    > But i am entering "Hello how are you?" and when i press Enter it displays the whole line. But it should display character wise.
    Input is by default line buffered, so what you're seeing is what is supposed to happen.
    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 Filster in forum C Programming
    Replies: 26
    Last Post: 08-03-2011, 04:45 PM
  2. getchar() and putchar()
    By kawaikx15 in forum C Programming
    Replies: 5
    Last Post: 04-13-2011, 11:02 AM
  3. getchar putchar
    By chess2009 in forum C Programming
    Replies: 7
    Last Post: 03-06-2011, 02:44 AM
  4. getchar and putchar
    By BEN10 in forum C Programming
    Replies: 4
    Last Post: 03-11-2009, 10:29 PM

Tags for this Thread