Thread: Kerningham Ritchie - example query

  1. #1
    Registered User
    Join Date
    Feb 2009

    Kerningham Ritchie - example query

    Below is a code snippet from Kerningham Ritchie book, chapter 8, section 8.2

    #include "syscalls.h"
    /* getchar: simple buffered version */
    int getchar(void)
    static char buf[BUFSIZ];
    static char *bufp = buf;
    static int n = 0;

    if (n == 0) { /* buffer is empty */
    n = read(0, buf, sizeof buf);
    bufp = buf;
    return (--n >= 0) ? (unsigned char) *bufp++ : EOF;

    I am not understanding why the bytes returned by read function stored in int n should be stored in a static int variable? How will in that case subsequent calls to getchar work? The read function is invoked only when n is equal to zero.

    Can someone please explain?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Notice that n is changed in subsequent lines.
    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
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    The first time you call the function, n is 0, so we go out to the operating system and get a whole pile of characters which we keep around in buf (since it's static, it will maintain value between calls). The next time through, we still have characters, so we return the next one (that's why bufp is incremented). Et cetera. Eventually, we will run out of characters (when n is 0), so we go out to the operating system and get another whole pile of characters. Lather, rinse, repeat.

  4. #4
    Registered User
    Join Date
    Feb 2009
    OKay.. I understand now.. I was wondering why we get a whole value of character, and return only one at a would require several getch() calls to return the whole buffer and after which only it would be able to take a new pile/single characters..

    this is just a getch() implementation to get a character from the buffer..


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. query problem
    By arian in forum C# Programming
    Replies: 1
    Last Post: 08-18-2008, 01:49 PM
  2. Url query encoding/decoding
    By Niara in forum Networking/Device Communication
    Replies: 6
    Last Post: 04-25-2007, 03:30 PM
  3. DNS Query
    By Simpsonia in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-24-2006, 12:42 AM
  4. dns query
    By bulldog in forum C Programming
    Replies: 6
    Last Post: 02-24-2004, 10:44 AM
  5. WBEM Query too slow
    By Unregistered in forum Windows Programming
    Replies: 0
    Last Post: 10-08-2001, 05:28 PM