getch()、getche()、およびgetchar()の違い



Difference Between Getch



まず、catch()を使用してヘッダーファイルconio.hを導入することを忘れないでください。私がC言語を学んでいたときは、常にプログラムの最後に追加して、一時停止後に実行されているプログラムの効果に使用するのが好きです。この文を追加しない場合、Ctrl + F9でコンパイルして実行するTC2.0環境では、プログラムが実行された後、TC環境に戻り、結果を確認する時間がないため、確認する必要があります。結果を確認するには、Alt + F5を押してDOS環境に戻るのは面倒です。また、プログラムの最後にgetch()の行を追加すると、実行後にプログラムが終了しないため、結果ステップでDOSを保存できますが、プログラムの最後で画面を停止するには、を押します。任意のキーTC環境に戻ります。次に、getch()がどのような効果をもたらすかを見てみましょう。 getch()は実際には入力コマンドです。 cin >>を使用する場合と同様に、プログラムは入力の待機を停止します。 cinとは異なり、getch()の役割はキーボードから文字を受け取ることであり、この文字を表示しません。つまり、キーを押した後、画面に押したものを表示せず、コードを実行し続けます。背後にあるので、C ++で実行できます。これを使用して、「任意のキーを押して続行」の効果を実現します。つまり、プログラムはこのステートメント行でgetch()を検出し、プログラムを一時停止して、任意のキーを押すのを待ちます。キーを押すと、文字キーを受け取り、コードビハインドの実行を続行します。


C ++のプログラムの最後にgetch()を追加しなかった理由を尋ねられるかもしれません。説明は、ソフトウェアは常に更新され、悪い場所はもちろん修正されるということです。 Getch()は、プログラムの最後に追加されます。どの変数にも割り当てられていないため、プログラムに関係なく、この場所では完全にジャンクコードです。これはC ++で考慮されるため、プログラムの実行が終了して終了しないたびに、画面が自動的に停止し、「任意のキーを押してください...」と表示され、終了するために任意のキーを押すように求められます。これは、C ++のようです。 。環境内でプログラムを実行し、プログラムの最後にgetch()ステートメントの行を自動的に追加し、outputステートメントの行を追加します。<<'press any key...' before the line statement to remind you that the program is over. , press any key to continue. In fact, our compiled program will not stop at the end of the program. We can find the compiled application (extension exe) in the compiled Debug directory, and double-click it in the folder to run it. Will find the screen flashed MS-DOS window is closed, because the program automatically exits after running, back to the windows environment, of course, if we run this program in DOS environment, we can directly see the DOS screen See the results of the program run, because the program does not clear the screen after running.


getch()、getche()に似たステートメントもあり、ヘッダーファイルconio.hを導入する必要があります。これらの違いは何ですか? [color = blue]違いは、getch()には戻り表示がなく、getch()には戻り表示があることです。 [/ color]なんて言うの?例を挙げましょう。
--------------------------------------
#include
#include
void main()
{
char ch
for(int i=0i<5i++)
{
ch=getch()
printf('%c',ch)
}
}

--------------------------------------
ここでは、入力と出力にC関数ライブラリを使用します。私はC ++のiostream.hを使用していません。これについては後で話します。まず、これは5回のループで5回の一時停止を実現します。入力をお待ちください。このプログラムをコンパイルして実行します。 abcdeを個別に入力するとします。画面に表示される結果はabcdeです。このabcdeはch = getch()にありません。出力では、この行のprintf( '%c'、ch)を削除し、任意のキープログラムを5回押しましたが、画面に何も表示されていません。


次に、コード内のgetch()をgetche()に変更して、何が違うのかを確認します。まだabcdeを個別に入力します。画面に表示される結果はaabbccddeeです。printf( '%c'、ch)を入力します。lineステートメントが削除され、結果はabcdeになります。これは、このステートメントが返されたときにプログラムがch = getche()を実行していることを示します。入力した内容が画面に表示されます。エコーの有無が唯一の違いです。 。


では、なぜC ++ライブラリが必要ないのかについて話しましょう。このプログラムをC ++に変更してみることができます。
--------------------------------------
#include
#include
void main()
{
char ch
for(int i=0i<5i++)
{
ch=getch()
cout< }
}

--------------------------------------
実行結果が完全に異なることがわかります。正直なところ、コンパイルと実行の方法がわかりません。私はそれをC ++で見つけて、続行する任意のキーの機能を実装していました。 [color = red]カウトがある場合<<'......' statement after the getch(), he will execute the following cout<<'......' then execute the getch() implement the pause, sometimes Adding a cout<

Cの関数ライブラリにあるので、削除する必要があると言う人もいます。私たちはまだそれを研究し、それでそれをしますか?しかし、私はそれがまだそれのために有用であるとわかりました、さもなければ私は皆の時間を遅らせるためにここであまり言いません。例を挙げましょう。手順は次のとおりです。
--------------------------------------
#include
#include

void main()
{
char ch='*'
while(ch=='*')
{
Printf(' press * to continue the loop, press other keys to exit!')
ch=getch()
}
Printf(' exit the program!')
}

--------------------------------------
[color = red]このループ本体に必要な関数を追加し、*を押してプログラム内のループを続行し、他のキーを終了し、エコーなしでgetch()の機能を使用できます。何を押してもかまいません。 。画面にトレースを残して、インターフェイスの見栄えを良くします。これを行うためのより良い方法がある場合、私はおそらくここでそれについて言及しません。あなたが本当により良い方法を持っているなら、私に知らせてください、ありがとう! [/色]


以下に、他の人のページにいくつかの例を次のように投稿します。
--------------------------------------
//Example 1: This example is to illustrate the difference between getch() and getche()
#include
#include

//Here is a small story: Because this code is on someone else's webpage, the C environment used by others may not require the conio.h header file.
/ / You can use getch () (I don't know), or may have forgotten to write, the source code on the page does not have #include this line,
//I let my wife go to see this page, my wife copied the code on the webpage into the C++ environment, and I can't compile and cry with me.
//Oh, my lovely silly wife!

void main()
{
char c, ch
c=getch() /*Reading a character from the keyboard is not echoed to the character variable c*/
Putchar(c) /*output the character */
Ch=getche() /*Read back a character from the keyboard and send it to the character variable ch*/
putchar(ch)
printf(' ')
}

--------------------------------------
//Example 2: This example is to complete the pause function during the process of demonstrating interactive input.
#include
#include
void main()
{
char c, s[20]
printf('Name:')
gets(s)
printf('Press any key to continue... ')
Getch() /*wait to enter any key*/
printf(' ')
}

--------------------------------------
//Example 3: The getchar() function also reads a character from the keyboard and brings it back. It differs from the previous two functions in that:
// The getchar() function waits for input until you press Enter. All input characters before the carriage return are displayed one by one on the screen.
// But only the first character is used as the return value of the function.
#include
#include
void main()
{
char c
c=getchar() /*Read characters from the keyboard until the end of the carriage return*/
//getchar() here it only returns the first character of your input string and assigns the return value to c
Putchar(c) /* shows the first character entered */
printf(' ')
}

--------------------------------------
//Example 4: Oh, this program you run, I believe you will have doubts.
#include
#include
void main()
{
char c
While ((c=getchar())!=' ') /* Each getchar() reads a character in turn*/
Printf('%c',c) /*output as is */
printf(' ')
}

--------------------------------------
4番目の例のプログラムが実行されたら、最初に停止し、文字列を入力するのを待ちます。入力が完了すると、入力した文字列全体が出力されます。ああ、getchar()が最初の文字だけを返すと言っているのではありません。なに、ここ?


心配しないでください、私はあなたにそれをゆっくり説明します、辛抱強く、そして私はそれをすぐに終えます。入力した文字列は最初の文字を取得せず、残りの文字列を破棄するためです。それはまだ私たちの記憶にあります。それは水を開けるようなものです。門に水を入れて開けます。ブレーキを解除すると解除されます。リリースされると、ライトがリリースされるまでリリースされます。入力する文字列も同じです。まず、入力した文字列がメモリのバッファに配置されます。一度呼んでいます。 Getchar()は、バッファ出力の最新の文字、つまり最初の文字出力を出力します。出力後、解放されますが、後ろに文字列があるため、前にループを使用します。文字は1つずつ解放されます。ループ条件を満たさずに終了するまでメモリ内に1つ。この例では、ループ条件の「 n」は実際には文字列を入力した後のキャリッジリターンであるため、キャリッジリターンが発生するまでループは終了せず、getchar()関数は入力を待機します。 Enterキーを押すまで。終了するので、文字列全体の出力が実装されます。もちろん、while((c = getchar())!= 'a')などのループ条件を変更することもできます。これは、文字 'a'に遭遇したときにループを停止することを意味します。