How getchar() is implemented?

2019-07-17 01:28发布

问题:

I was just wondering how getchar() is implemented? Is it something like the following? It is quite inefficient to read this way, single byte. Does it use some buffering?

Pseudo code:

int getchar() {

    char buf[1];
    int n = read(0, buf, 1);

    if(n < 1)
        printf("Read failed");

    return buf[0];
}

回答1:

Considering the number of implementations for the standard C library, it's not possible to provide a definite answer, but the most common ones seem to follow the same general guidelines.

By definition, getchar() uses the stream infrastructure of the standard C library i.e. FILE and its friendly functions. In most modern C library implementations, the file streams are buffered to a degree, with the buffer size and behavior usually being tunable via setvbuf().

I am aware of at least one case (glibc) where files may optionally - via an extra option to fopen() - be accessed via memory mapping (i.e. mmap()) rather than read()/write(). To avoid issues when mixed with calls to higher level functions, such as scanf(), getchar() is forced to use the same buffering structures.

Without information from a profiler, I'd worry more about the structural complexity of any code that uses getchar() than about any performance issues caused by its use.



回答2:

Here is a very simple implementation.

int mygetchar(void)
{
        static char buf[BUFSIZ];
        static char *bufp = buf;
        static int i = 0;

        if (i == 0)
        {
                i = read(0, buf, 1);
                bufp = buf;
        }
        if ( --i >= 0 )
        {
                return  *bufp++;
        }

        return EOF;
}


标签: c stdio