Let's talk about the basic differences.
(1) getch () and getche () Functions
Both functions read one character from the keyboard. The call format is:
Getch ();
Getche ();
The difference between the two is that the getch () function does not display the characters read back on the display screen, while the getche () function does ()
The function returns the characters read to the display screen.
Example 1:
# Include <stdio. h>
Main ()
{
Char C, ch;
C = getch ();/* read a character from the keyboard and do not return it to the character variable C */
Putchar (c);/* output this character */
Ch = getche ();/* read a character explicitly from the keyboard and send it to the character variable Ch */
Putchar (CH );
}
With the features of ECHO and non-Echo, these two functions are often used to complete the pause during interactive input.
.
Example 2:
# Include <stdio. h>
Main ()
{
Char C, S [20];
Printf ("name :");
Gets (s );
Printf ("press any key to continue ...");
Getch ();/* wait for any input key */
}
(2) getchar () function
The getchar () function also reads a character from the keyboard and brings it back to the display. It corresponds to the first two functions.
The difference is: The getchar () function waits for the input to end until Press enter. All input words before press ENTER
Are displayed on the screen one by one. But only the first character is used as the return value of the function.
The call format of the getchar () function is:
Getchar ();
Example 3:
# Include <stdio. h>
Main ()
{
Char c;
C = getchar ();/* read characters from the keyboard until the carriage return ends */
Putchar (c);/* display the first character of the input */
Getch ();/* wait for any key */
}
Example 4
# Include <stdio. h>
Main ()
{
Char c;
While (c = getchar ())! = '/N')/* Each getchar () reads one character in sequence */
Printf ("% c", c);/* output as is */
Getch ();/* wait for any key */
}
Let's talk about the cause of getch (); at the end of the file.
First, do not forget. To use getch (), you must introduce the header file conio. h. When we used to learn C language, we always liked to add it at the end of the program and use it to achieve the effect of pausing and not exiting after the program is run out. If this sentence is not added, after we use Ctrl + F9 to compile and run the program in the tc2.0 environment, the program will be returned to the TC environment after it is run, and we cannot see the result at all, at this time, we need to press Alt + F5 to return to the DOS environment to view the results, which is very troublesome. If you add a getch (); statement at the end of the program, we can skip the step of DOS viewing results, because the program does not exit after running, instead, the screen is stopped at the end of the program. Press any key to return it to the TC environment. Let's take a look at the role of getch (). getch () is actually an input command, just as when we use CIN>, the program will stop waiting for you to enter it, unlike Cin, getch () receives a character from the keyboard and does not display it. That is to say, after you press a key, it does not show what you press on the screen, but continues to run the subsequent code, therefore, we can use it in C ++ to achieve the "continue by any key" effect, that is, if the program encounters getch (); this line of statements, it will suspend the program, when you press any key, it receives the character key and continues to execute the subsequent code.
You may ask why we didn't add getch () at the end of the program in C ++. The explanation is that the software is constantly updated, and the correction must be made in poor cases, when getch () is added at the end of the program, it is not assigned to any variable, so it is completely spam code in this place and has nothing to do with the program. C ++ automatically stops the screen and displays "press any key... "Ask you to press any key to exit. This is like C ++ running a program in its environment. A getch () Statement is automatically added at the end of the program, A line of output statement cout <"press any key... "; to prompt you that the program is over. Press any key to continue. In fact, the compiled program will not stop after the program is completed. We can find the compiled application (extension exe) in the compiled Debug directory ), double-click in the folder to run it, you will find that the screen flashed a MS-DOS window closed, because the program runs out automatically quit, back to the windows environment, of course, if we run this program in the DOS environment, we can directly view the program running result on the DOS screen, because the screen is not clear after the program runs.
Another statement is similar to getch (). getche () also needs to introduce the header file conio. h. What is the difference between them? The difference is that getch () has no returned display, and getche () has returned display. What should I do? Let me give you an example.
--------------------------------------
# Include <stdio. h>
# Include <conio. h>
Void main ()
{
Char ch;
For (int I = 0; I <5; I ++)
{
Ch = getch ();
Printf ("% C", CH );
}
}
--------------------------------------
Here I use the C function library for input and output, and I do not use C ++ iostream. h. I will wait for a while. First of all, this is a 5 consecutive loop to implement 5 pauses. Wait for us to input, compile and run this program. Suppose we enter ABCDE separately, and the result displayed on the screen is ABCDE, this ABCDE is not output in CH = getch ();. We will remove this line of statements from printf ("% C", CH, we will find that the program ended after pressing any key five times, but nothing is displayed on the screen.
Then, let's replace getch () in the Code with getche () to see what is different. We enter ABCDE separately. Then, the result displayed on the screen is aabbccddee, let's remove this line of printf ("% C", CH); the result is ABCDE, indicating that the program is executing CH = getche (); when this statement is used, the keys we enter are displayed on the screen. The only difference between them is that there is no echo.
Let's talk about why C ++ function libraries are not used. You can try to change this program to the C ++ format:
--------------------------------------
# Include <iostream. h>
# Include <conio. h>
Void main ()
{
Char ch;
For (int I = 0; I <5; I ++)
{
Ch = getch ();
Cout <ch;
}
}
--------------------------------------
You will find that the running results are completely different. To be honest, I don't know how it is compiled and run, in the past, I used it in C ++ to implement the function of resuming any key. If getch (); is followed by a cout <"...... "; The statement will first execute the following cout <"...... "; Then execute getch (); To implement the pause. Sometimes a cout <endl; is added to the middle of the two statements to solve this problem, but if printf () in C is used () this problem has never occurred. I don't know why, but I can only guess, probably because getch () is a function in C's function library and is not very useful in C ++, that is to say, it is a problem of compiling the system itself, and it has nothing to do with the program we write. I don't know if my analysis is correct. I hope the experts can give some advice. Thank you!
Some people will say that since it is in the C function library, it should be eliminated. We will also study it and what should we do with it? However, I found that it is still useful, otherwise I will not say so much here to delay everyone's time. For example, the program is as follows:
--------------------------------------
# Include <stdio. h>
# Include <conio. h>
Void main ()
{
Char ch = '*';
While (ch = '*')
{
Printf ("/n press * to continue the loop, press another key to exit! ");
Ch = getch ();
}
Printf ("/n exit the program! ");
}
--------------------------------------
We can add the function we want in this loop body. Press * in the program to continue the loop, and any other key will exit, and the getch () feature is used without echo. No matter what we press, will not leave any trace on the screen, so that our interface can achieve beautiful results, if there is a better way to achieve this function, I may not mention so much here. If you have a better solution, please let me know. Thank you!
Below I will repeat several examples on others' web pages as follows:
--------------------------------------
// Example 1: This example illustrates the difference between getch () and getche ().
# Include <stdio. h>
# Include <conio. h>
// Here is a short story: Because the code is on someone else's webpage, the C environment used by others may not need the conio. h header file.
// You can use getch (); (I don't Know), or you may forget to write it. The source code on the webpage does not include <conio. h>,
// I asked my wife to visit the webpage. My wife copied the code on the webpage to the C ++ environment and cried with me if the Code cannot be compiled,
// Haha, my lovely silly wife!
Void main ()
{
Char c, ch;
C = getch ();/* read a character from the keyboard and do not return it to the character variable c */
Putchar (c);/* output this character */
Ch = getche ();/* read a character explicitly from the keyboard and send it to the character variable ch */
Putchar (ch );
Printf ("/n ");
}
--------------------------------------
// Example 2: This example shows how to pause the interactive input.
# Include <stdio. h>
# Include <conio. h>
Void main ()
{
Char c, s [20];
Printf ("name :");
Gets (s );
Printf ("press any key to continue.../n ");
Getch ();/* wait for any input key */
Printf ("/n ");
}
--------------------------------------
// Example 3: The getchar () function also reads a character from the keyboard and brings it back to the display. It differs from the previous two functions in the following way:
// The getchar () function waits for the input to end until Press enter. All input characters before press enter are displayed on the screen one by one.
// But only the first character is used as the return value of the function.
# Include <stdio. h>
# Include <conio. h>
Void main ()
{
Char C;
C = getchar ();/* read characters from the keyboard until the carriage return ends */
// Getchar () Here it only returns the first character of your input string, and assigns the return value to C
Putchar (c);/* display the first character of the input */
Printf ("/n ");
}
--------------------------------------
// Example 4: Haha, you have run this program. I believe you will have questions again.
# Include <stdio. h>
# Include <conio. h>
Void main ()
{
Char C;
While (C = getchar ())! = '/N')/* Each getchar () reads one character in sequence */
Printf ("% C", c);/* output as is */
Printf ("/n ");
}
--------------------------------------
In Example 4, when the program is running, stop and wait for you to input a string. After the input is complete, it outputs the entire string you have entered. Then, you do not mean getchar () is the first character returned?
Don't worry. I'll explain it to you slowly. Please wait a moment and I'll finish talking about it right away. Because the input string does not take the first character, it will discard the remaining string. It is still in our memory, just like opening the gate and releasing water, after we put the water in the gate, we open the gate once and then let it off a little. Once we open it, we let it off a little until it is cleared. The same is true for the string we input, first, the input string is placed in the memory buffer. We call getchar () to output the most recent character in the buffer, that is, the first character output, after the output, it is released, but there are strings behind it, so we use a loop to release the first character one by one in the memory, exit until the loop condition is not met. In the example, '/N' in the loop condition is actually the carriage return after you input the string, so it means that the loop ends until a carriage return occurs, while getchar () the function is waiting for the input to end until press enter, so the output of the entire string is realized. Of course, we can also change the loop condition, such as while (c = getchar ())! = 'A.