Getch () and getche () getchar ()

Source: Internet
Author: User

(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 getche ()
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)
}
The echo function and the non-echo function are often used to pause the 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 display. It corresponds to the first two functions.
The difference is that the getchar () function waits for the input to complete all input words before the carriage return is completed until 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 ()/reads 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 we add a getch () statement at the end of the program, we can save the step of viewing the result by DOS because the program does not exit after running, but stops the screen at the end of the program, press any key to return to the TC environment. Let's take a look at the role of getch (). getch () is actually an input command, just like we use CIN>
> When the program stops, the program waits for you to input. Unlike Cin, getch () is used to receive 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 effect of "continue by any key", that is, if the program encounters the getch () statement, 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, in addition, a line of output statement cout <"press any
Key... "to prompt you that the program is finished. 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 it in the folder and you will find that the MS-DOS window is closed after a screen flash, because the program automatically exits after the program runs, and returns 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 printf ("C" ch) statements, and we will find that the program is finished after five times of pressing any key, 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) Statements and check that the displayed 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 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. However, 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 continue loop, press another key to exit! ")
Ch = getch ()
}
Printf ("\ n quit the program! ")
}
--------------------------------------
We can add the functions we want in this loop body, press continue loop in the program, and any other key exits, and use the getch () feature 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 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 ()/reads 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' is actually the carriage return after you input the string, so it means that the loop is not completed until the carriage return is encountered, and the getchar () function is waiting for the input to end until you press Enter, therefore, the output of the entire string is realized. Of course, we can also change the loop condition, such as while (C = getchar ())! = 'A.

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.