Thread: Getchar() usage...

  1. #1
    Registered User
    Join Date
    May 2002

    Question Getchar() usage...

    I'm just learning C and was trying to run the following basic example below; problem is that it doesn't do what it is supposed to, instead of reading a character from the kb and display it, it reads an input string (until I pres <ENTER>) and then it displays it. What's wrong?
    The following is quoted from the book I'm learning from:

    The following program illustrates this,

    #include <stdio.h>

    int i, ch;

    for( i = 1; i <= 5; ++i ) {
    ch = getchar();

    Sample Program Output

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Waterloo, Texas

    setbuf( fp, NULL );


    setvbuf( fp, NULL, _IONBUF, 0 );

    then getchar()...

    Neither, however worked on my system(Windows), though their supposed to...

    BTW: the first returns no value, the second zero on success...
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
        return std::pow
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Setbuf affects the stream file buffer (stdin) but has no effect on the keyboard buffer.

    The getchar function reads a character from the keyboard buffer. You can only read from this buffer when it's flushed by a carriage return or when it's completely filled. To read characters directly from the keyboard (in stead of the keyboard buffer) you can use the getch function (if it's supported).

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Sample Program Output
    Does the book specify this output? If so the author assumes that your system has no buffering of the input stream, which is pretty stupid since most platforms use a buffering system. To get the correct output that the author expects, you need to change the program slightly and make it less standard:
    #include <conio.h> 
    int main ( void ) 
      int i, ch; 
      for( i = 1; i <= 5; ++i ) { 
        ch = getche(); 
        putch ( ch ); 
      return 0;
    Note that his program may not work on your compiler if conio is not supported. You might want to get another book.

    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    May 2002

    Smile Thanks...

    Thanks Monster and Prelude, now I understand what went wrong. Your modified example works fine.

    I think I'll swap this Brian Brown C Programming Course with K&R (other suggestions maybe...?), since it's a shame to get lost like that in simple aspects of C... at least when I make a mistake of my own I now it's my fault but this little part of code did cost me a lot of time because it's used later in the book in some much complex data validation examples.

    I imagine how bad it can get with pointers & stuff if such simple code is completely misunderstood by the author...

    Thanks again guys, keep up the good work!

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