Deep analysis of _c language with GetChar caching mechanism

Source: Internet
Author: User
Tags stdin

The most common operation associated with buffer cache is the input and output operations of characters Getchar,getc,getch,getche,gets series functions.

The first example (relating to GetChar):

Copy Code code as follows:

#include <stdio.h>
int main ()
{
int ch;
Ch=getchar ();
Ch=getchar ();
printf ("%d\n", ch);
return 0;
}

Code as above, when the input of a character press ENTER after the program did not wait for you two input on the end, and no matter what type of run results are 10, is it strange (anyway I felt strange when I first met) and, even more strangely, when you entered multiple characters at a time, Furu the ABCD results and correctly printed out 98, Why, then? This is the reason for the buffer zone.

The explanation is as follows: GetChar defined in the stdio.h file, we can find its relevant definition in stdio.h:

Copy Code code as follows:

#define GETCHAR () getc (stdin)//is GetChar equivalent to calling GETC (stdin)

And we found the definition of getc.
Copy Code code as follows:

#define GETC (_stream) (--(_stream)->_cnt >= 0 \
? 0xFF & * (_stream)->_ptr++: _filbuf (_stream))

To expand it:
Copy Code code as follows:

if (--(stdin)->_cnt>=0)
Return 0xff&* (stdin)->ptr++;

Copy Code code as follows:

Else
Return Filbuf (stdin);

The code translates as follows stdin is a standard input stream, and viewing MSDN and stdio.h can be seen in the following definitions:

In Stdio.h:

Copy Code code as follows:

#define STDIN (&_iob[0])

Tracking can be:
Copy Code code as follows:

_crtimp extern FILE _iob[];

From the above code can be _IOB is a file structure type, see stdio.h can see the file structure defined as follows:
Copy Code code as follows:

struct _IOBUF {
Char *_ptr;
int _cnt;
Char *_base;
int _flag;
int _file;
int _charbuf;
int _bufsiz;
Char *_tmpfname;
};


From the file structure we can get the _CNT,_PTR members used in the GETC macro definition above, but these are secondary, we should not be difficult to find that there are several members _bufsize,_base corresponding to the buffer size, buffer base address, The conclusion from this view is that the GetChar function uses a buffering mechanism. (_cnt corresponds to the number of bytes entered in the buffer, _ptr corresponds to the position of the read pointer)

GETC macro Definition Detailed

Copy Code code as follows:

--(stdin)->_cnt>=0

This sentence to determine whether there is data in the buffer, some words to reduce one (to read another), and read the data Return 0xff&* (stdin)->ptr++, read the completion, the reading pointer forward one position "important"

OK, so much is the foreshadowing, now back to the point why the above results:

The above results traced or because the GetChar function uses a buffer (see above, I think we also know that the buffer is really used, when the input of a character to press ENTER after the program did not wait for you two input to the end, and regardless of the input of any operating results are ten, This is because when a user enters a character, and press ENTER, the buffer will be stored in the user input characters as well as the line-changing ASCII code (10 ~) "omitted the return of the ASCII code 13, may be to cross the platform, in Linux, after the return is a line of 10, Windows next car is the first return to the first line, and then the line, that is 13 10 "(The first time GetChar is actually executed getc else statement, fill the buffer after _cnt=2,_prt point to 0 position, after the completion of _cnt=1,_prt point 1 position), The second time the GetChar is executed, the call GETC no longer executes the Else statement, executes the IF statement, so the second time no longer waits for the user to enter, executes directly, executes after cnt=0,prt points to position 2, and returns the PTR point to position 1 o'clock result, namely 10, because 10 is the newline key, When execution encounters this time, the cache is emptied after execution, and PTR is pointed back to the position 0,cnt=0.

When the ABC is entered, the analysis is the same, but after sweeping the second getchar, cnt=2,ptr points to position 2.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.