Kerningham Ritchie - example query

This is a discussion on Kerningham Ritchie - example query within the C Programming forums, part of the General Programming Boards category; Below is a code snippet from Kerningham Ritchie book, chapter 8, section 8.2 #include "syscalls.h" /* getchar: simple buffered version ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    6

    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
    Location
    Singapore
    Posts
    21,602
    Notice that n is changed in subsequent lines.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Posts
    14,185
    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
    Posts
    6
    OKay.. I understand now.. I was wondering why we get a whole value of character, and return only one at a time..it 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..

    thanks.

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, 09:44 AM
  5. WBEM Query too slow
    By Unregistered in forum Windows Programming
    Replies: 0
    Last Post: 10-08-2001, 05:28 PM

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