Password does not echo function (i) __ function

Source: Internet
Author: User
Getch getche GetChar Difference and buffer concept
Today, my classmate asked me a question. NET compile C program The final result is always a flash. Remember that there is a function to achieve its function, so the relationship between these functions is not clear, summed up.
1. The concept of input and output buffers (more C + +)I would like to illustrate, for example, that I want to output an article in a sequence of characters to a computer monitor screen, then my program memory as a data source and the display driver as the data target,
If the data source sends data directly to the data destination. The data target gets the first character and displays it. And then read the next character from the port,
However, there is no guarantee that the data source sends to the port exactly the second character (perhaps the third, and the second is already sent when the data target is displayed).
In this way, the output data can not be guaranteed to be fully accepted and processed by the data target.
In order to solve this problem, we need to put a data source and data target in the middle of the area to save the full data content, that is, "buffer." In that case,
Data source can not consider the data target is processing which part of the data, as long as the data output to the buffer can be, the data target can also not consider the data source to send frequency, but from the buffer in order to retrieve the next data.
This ensures the integrity of the data sent, but also improves the efficiency of the program.
Of course Getch (), Getche () does not use a buffer.
2. The difference between several functionsFirst of all, do not forget, to use Getch () must introduce the header file Conio.h, before learning C language, we always like to use at the end of the program plus it, use it to implement the program run out of pause does not quit the effect.
If you do not add this sentence, in the TC2.0 environment we compile and run with CTRL+F9, after running the program back to the TC environment, we simply can not see the results, at this time to see the results,
We need to press ALT+F5 back to the DOS environment to see the results, which is troublesome. And if you add a line of Getch () to the end of the program, we can skip the DOS look at the result.
Because the program does not quit after running, but at the end of the program to stop the screen, press any key to return to the TC environment.
So let's see what the Getch () really does, getch () is actually an input command that receives a character from the keyboard and does not display the character, which means
You press a key after it does not display what you press on the screen, and continue to run the following code, so we can use it in C + + to achieve the "press any key to continue" effect, that the program encountered Getch ();
This line of statements, it will suspend the program, and so you press any key, it received the word keys, and then continue to execute the following code.
You may ask why we did not add getch () to the end of the program in C + +, the explanation is that the software is always updated, the bad places of course to be corrected, getch () added at the end of the program,
It's not assigned to any variable, so it's completely junk code in this place, and it's not about the program. C + + to take this into account, so that every time the program runs out of the exit, but automatically stop the screen,
And the "Press any key ..." is called to exit by pressing any key, which is like C + + running the program in its environment and automatically adding a line of getch () to the end of the program.
And before this line statement also added a line of output statements cout<< "Press any key ..." to prompt you to end the program, pressing any key to continue.
In fact, we compile the program at the end of the program itself will not stop, we can be compiled in the debug directory to find this compiled application (extension EXE), in the folder double-click to run it,
You'll notice that the screen is flashing. MS-DOS windows are closed because the program automatically exits when it is finished and returns to the Windows environment, of course, if we run this program in a DOS environment,
We can see the results of the program running directly on the DOS screen, because the program does not clear the screen when it is finished running. However, if visual stdio.net2003 has returned to TC, you need to have a getch ().
Getche () and Getch () are similar, it also needs to introduce header file Conio.h, then the difference between them. The difference is that getch () has no return display, Getche () has a return display.
Just a little bit. Take a look at the following example:
[CPP]View plain copy #include <stdio.h> #include <conio.h> void Main () {char ch;           for (int i=0;i<5;i++) {ch=getch ();       printf ("%c", ch); }   }

First of all, this is a 5 consecutive loop to achieve 5 pauses, waiting for us to input, we compile and run the program, assuming that we enter ABCDE, the screen will display the result is ABCDE,
This ABCDE is not in the Ch=getch (), we put printf ("%c", ch), this line of statements, we will find that we press the 5-time arbitrary key program is over, but the screen does not show anything.
Then we replace the Getch () in the Code with Getche () to see what the difference is, we enter ABCDE separately, and the result on the screen is Aabbccddee, we put printf ("%c", ch);
This line of the statement again take a look, the result of the display is ABCDE, the program in the execution of Ch=getche (), the statement of the input we entered the key back to display on the screen, there is no echo is their only difference.
Some people will say, since it is a function of C library, then should be eliminated, we also study it, and what to do with it. But I found it still useful place, otherwise I will not be here to say so much to delay everyone's time.
Let me give you an example, the procedure is as follows:
[CPP]View plain copy #include <stdio.h> #include <conio.h> void Main () {char ch= ' * '; while (ch== ' * ') {printf ("N/A * press * to continue the loop, press the other key to exit.)           ");       Ch=getch (); printf ("N/A) quits the program.   "); }

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.