Hi!
Do I understand the following code correctly?
Code:
int getchar(void)
{
static char buf[BUFSIZ];
static char *bufp = buf;
static int n = 0;
if (n == 0)
{ /* buffer is empty */
n = read(O, buf, sizeof buf);
bufp = buf;
}
return (--n >= 0) ? (unsigned char) *bufp++ : EOF;
}
If character array buf is empty (if (n == 0), the function read()
will read a character string of a size sizeof(buf) which is equal to
the size of declared array buf. So, this array buf after read () is done
will contain a few characters.
I am not sure I understand the return statement:
Code:
return (--n >= 0) ? (unsigned char) *bufp++ : EOF;
If after decrementing n the value of n is bigger or equal to 0, then
the program returns the first character of the array buf (buf[0]) to
which bufp points to. Then bufp is incremented to point to the next
character. How does the whole inputted array gets printed? It seems
that only one character is printed.
(If error, the program returns EOF; it's clear).
Thanks for your help!