C language input and output buffers concept

Source: Internet
Author: User

the concept of a language input and output buffer you're going to wonder why you start off with this, it's not all data type at first, it's written in the front because the program even the simplest code will use the input and output, the output is relatively simple, can be put in the back, but the input is different, If you don't know it first, you might get a different result than you expected.^_^. It is because and the General C language Introduction way different, in order to look normal, I will call this chapter CHAPTER0, can jump to the past, directly see Chapter1.1. GetChar first Quote from previous achievements (modified)^_^: http://blog.csdn.net/cxyol/archive/2006/03/18/628324.aspxGetChar () is a library function in stdio.h that reads a character from the stdin stream, meaning that if the stdin has data, it can be read directly without input. Getch () and Getche () are library functions in conio.h, which function to receive characters from the keyboard.    GetChar with display. The difference from the previous two functions is that the GetChar () function waits for input until it ends (provided the buffer has no data), and all input characters before the carriage return are displayed on the screen one by one. But only the first character is the return value of the function. #include<stdio.h>#include<conio.h>voidMain () {CharC; C=GetChar (); //GetChar () Here it only returns the first character of your input string and assigns the return value to CPutchar (c); printf ("\ n");} This program you run a bit, I believe you will have doubts. This is the example read from the buffer. The first time GetChar () does require manual input, but if you lose more than one character, the subsequent getchar () will be read directly from the buffer when it is executed. #include<stdio.h>#include<conio.h>voidMain () {CharC;  while((C=getchar ())! ='\ n') printf ("%c", c); printf ("\ n");} When the program runs, first stop, wait for you to enter a string, after the input, it put you input the entire string is lost, gee, you do not say GetChar () only return the first word Fu Yi, here how? Because we entered the string is not take the first character to throw away the rest of the string, it is still in our memory, like, the opening of the water, we put in the brakes to go after, open a brake will be put off a point, open once a little, until the light up so far, Here the opening action is equivalent to calling a GetChar (). The string we entered is the same thing, first we enter the string is placed in the memory buffer, we call once GetChar () in the buffer in the exit of the nearest character output, that is, the first character output, after the output, it was released, but there are strings behind, So we just use the loop to release the first character one after another in memory until the loop condition is not met. In the case of a cyclic condition.'\ n'It is actually the carriage return after you enter the string, so it means that the loop is not finished until the carriage return is encountered, and the GetChar () function waits for input (or data in the buffer) until it ends with a return, so the output of the entire string is implemented. Of course, we can also change the loop conditions, such as while ((C=getchar ())! ='a'), what do you mean, it means encountering characters'a'stopping the loop, of course, means that if you type "12345a213123\n" it will only output to a, and the result is 12345a. Note again: With GetChar () it is read from the middle of the stream, so the first GetChar () accepts the first character that is about to be enqueued in the stream queue that is just interrupted (not limited to the carriage return, above example), and if the flow queue is not empty, execute GetChar () to continue to drain, Until the carriage return is also empty, and then after the execution of GetChar () to stop waiting for your input, we use Getch () Why each time is waiting for the user input it?    Because Getch () is received from the keyboard, instant reception, not from the stdin stream to read the data. Add: Press Enter on keyboard to generate 2 characters: Carriage return ('\ r') and line Break ('\ n')。 Carriage return character'\ r'(Cr:carriagereturn: reversing) causes the cursor to return to the header of this line, the line break ('\ n')(Newline) and then wrap. So when the input character'W', and press ENTER after. First you get a carriage return character. The GetChar function is over. But there is also a newline character. So if you use GetChar () to make judgments. Better write one more time. GetChar () clears the buffer'\ n'.3.    How do I empty the contents of an input buffer? If I want GetChar () to be able to wait for the user input every time to clear the buffer, the following introduction method (different platform) C standard specifies that the Fflush () function is used to refresh the output (stdout) cache. For input (stdin), it is undefined. But some compilers also define the implementation of Fflush (stdin), such as Microsoft's VC. Whether other compilers also define the Fflush (stdin) implementation should look for its manual.       The GCC compiler does not define its implementation, so you cannot use Fflush (stdin) to flush the input cache. For compilers that do not have a fflush (stdin) defined, you can use the fgets () function instead (more versatile than GetChar (), scanf (), and so on. Other inputs, such as the carriage return left in the input stream, can be ignored so that the next input is always kept in a "clean" state. (This is available under any platform)Charsbuf[1024x768];// ...Fgets (Sbuf,1024x768, stdin);// ...You can do this under the VC on Windows: for(intI=0;i<Ten;++i) {       CharCh=GetChar (); Fflush (stdin); //There's always a waiting state.It says here that the GCC compiler does not define FFLUSH implementations, and we generally use GetChar () to clear the buffers. Here is my discussion: First, a code: #include<stdio.h>Main () {CharC;  for(;(C=getchar ())! ='a';) printf ("%c", c);     GetChar (); C=GetChar (); printf ("%c", c);} Input: ssss carriage return: SSSS cursor (wait for input) Description: At this point, the program does not end, to the for loop, because there is no character a appears, so did not jump out for the for loop. After you type carriage return, GetChar is removed from the buffer (for loop) in turn:'s"'s"'s"'s"'\ n'If we enter: Ssssa carriage return to get: SSSS cursor (wait for input) Description: The program has jumped out for the for loop, but because we use GetChar ();'\ n', the 7th sentence in the back c=GetChar (); requires you to enter a character (because there is no new character behind Ssssa), so the program is still not finished. If we comment out GetChar (); this sentence, then get: SSSS cursor (program end) This input ssssa is the line break in the carriage return '\ n'It was c=.GetChar (); This sentence reads and outputs. Summary: Keyboard input characters are stored in the buffer, once you type Enter, GetChar into the buffer to read the characters, only return the first character as the value of the GetChar function at a time, if there is a loop or enough GetChar statement, it will read out all the characters in the buffer until'\ n'To understand this, the sequence of characters you enter is read sequentially because the function of the loop is to reuse GetChar to read the characters in the buffer, instead of GetChar to read multiple characters, in fact GetChar can only read one character at a time. If you need to cancel'\ n'The effect can be used GetChar (); To clear, here GetChar (); just made'\ n'However, it is not assigned to any character variable, so there is no effect, which is equivalent to erasing the character. Also note that here you enter SSSS on the keyboard to see the echo is from the role of GetChar, if you use getch can not see what you entered. One more article: http: //www.cnblogs.com/biser/archive/2004/09/23/45704.aspx1. Mechanism you entered something on the keyboard, and at this time you did not use the program to getchar her, ask this time you press the state of the key to save where?Why do you get it when you go to GetChar for a while (for example, you do a 1-minute delay, and then you getchar, you'll notice that what you press a minute ago is displayed) is actually the input deviceMemory buffers,program GetChar You press the key is put into the buffer, and then for the program GetChar you have not tried to hold a lot of keys and then wait a moment will drip drops, is full of buffer, you press the key is not stored in the buffer.2. GetChar () and Getch () and then I can tell you GetChar is the return to the buffer after the getch is every time into the buffer with your program (I think it should be \ n not/N) In fact, you enter the computer, did not press ENTER before the operation is stopped in the GetChar (), not into the loop, naturally did not run printf when you press ENTER, only from GetChar out, and then because the keyboard buffer inside something, Just one character getchar out. Want to immediately echo, with Getch just fine2. scanfscanf This library function is rather strange, and there are some flaws, so many people do not need, here is still a brief introduction. scanf input string, integer, real type and other data to determine the same way, enter, space, tab is considered to be the end of a data, Of course character words, a character is the end of, enter, space, etc. have the corresponding ASCII code, so use scanf input characters to be careful when these things are entered as characters, and input string and integer type, These are treated as delimiters and are not entered into character arrays or variables. Of course, if the input format is not"%s%s"But"%s,%s"The delimiter is a comma, and this is said when I say the input and output function. So many examples: #include<stdio.h>intMain () {Charn1[Ten];Charn2[Ten];scanf ("%s", N1); scanf ("%s", N2);p rintf ("n1=%s,n2=%s", n1,n2);} Input: Hello Enter world enter get: N1=hello,n2=wolrd Cursor (program end) Here Hello is followed by the input and multiple carriage returns, the spaces will not be assigned to the N2, because they are just delimiters. If input: Hello enter cursor (wait for input) indicates that carriage return is recognized as a delimiter, So the program also wants you to enter a string to assign to N2. In fact, in the buffer there is a'\ n'to be left behind, the program changes to this: #include<stdio.h>intMain () {Charn1[Ten];Charn2[Ten];Charn3,n4;scanf ("%s", N1); scanf ("%s", N2);p rintf ("n1=%s,n2=%s", N1,N2); N3=GetChar ();p rintf ("%c", N3);//N4=getchar ();//printf ("%c", N4);} Input: Hello Enter world Enter to get: HELLOWORLDN1=hello,n2=wolrd Cursor (program end) If you cancel the last two lines of comment, the same input gets: HELLOWORLDN1=hello,n2=wolrd at the cursor (waiting for input) indicates that there is only one in the buffer'\ n', the second getchar need you to enter a character again, there is no character in the buffer. SCANF does not assign a carriage return space to a string but assigns it to a character, just like GetChar, it is time to consider'\ n'the existence of. For example: #include<stdio.h>intMain () {Charn1[Ten];Charn2;scanf ("%s", N1); scanf ("%c",&n2);p rintf ("n1=%s,n2=%d", n1,n2);} Input: Hello enter get: N1=hello,n2=10 at cursor (end of program)//10 is the ASCII code of ' \ n '.If you enter: Hello space carriage return (must have a carriage return, because scanf is also to wait for carriage return, exactly said is'\ r'to read the buffer.) Gets: N1=HELLO,N2=32 at cursor (end of program)//32 is the ASCII code for the space.Again, if the last sentence input n2=%d changed to n2=%C, then enter: Hello Enter to get: N1=hello,n2=at the cursor (program end) is not as getchar as can be'\ n'read it, hehe. Summary is: If the scanf input is not a character, then the delimiter is a carriage return, a Space, TAB key, two data between the delimiter is only the difference between two data, the separation of the two data is assigned to the respective defined variables or arrays, The delimiter between the two data is read out of the buffer but does not play any role, of course the last'\ n'Will be left in the buffer zone unless GetChar () is used, or scanf ("%c",&c) Read it out. A carriage return is a must, no matter getchar or scanf. Functions that enter data through a buffer are waiting for the Enter key'\ r'occurs before the buffer is entered. A mixture of integer data, strings, and characters: #include<stdio.h>intMain () {inta,b,c;Charn1[Ten];Charn2,n3;scanf ("%d%d",&a,&b); scanf ("%c",&n2); scanf ("%d",&c); scanf ("%s", N1); scanf ("%c",&n3);p rintf ("a=%d,b=%d,n2=%c,c=%d,n1=%s,n3=%c", a,b,n2,c,n1,n3);} Input: A(several spaces or carriage returns do not affect the result, this uses a carriage return) the(input is also required because scanf only has an integer data, and there is no integer data in the buffer.) To have a carriage return or a space indicates that the data is over, leaving the space or carriage return to the next%c Accept, here with a carriage return, you can try the space) $JFDKJFA (carriage return) received: a= A, b= the, n2=, C= $, n1=jfdkjfa,n3=At the cursor (program end) here to illustrate the process: in the first two integer data input, the two data between the carriage return or a number of spaces are scanf as delimiters, OK, scanf read the delimiter (carriage return or space), the first integer data is sent to the variable A, The buffer leaves the delimiter and the following integer data, at which point the scanf reads the delimiter first, but the input or integer data is required (%d), so the delimiter is ignored if the input character%c (not the string%) is requireds), then the delimiter will be sent to the character variable as a byte, just like the N2 here. The same can be known about the save process of C and N1, the last N3 is to receive the input of the last carriage return. Okay, well, if you see this, you understand that. The last example: #include <stdio.h>Main () {intA;Charch;scanf ("%d",&a); CH=GetChar ();p rintf ("%d,%c", a,ch);} Input: 95 return car get: the, at the cursor (program end) It is obvious that this is because the delimiter (carriage return) is GetChar read and output, if added a sentence: GetChar (); #include<stdio.h>Main () {intA;Charch;scanf ("%d",&a); GetChar (); Ch=GetChar ();p rintf ("%d,%c", a,ch);} Input: 95 return car C Enter to get: the, c cursor at (end of program)
Http://blog.sina.com.cn/s/blog_51409e8f01009han.html

C language input and output buffers concept

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.