logo

C の Getchar() 関数

このセクションでは、C プログラミング言語の getchar() 関数を学習します。あ getchar() 関数は 規格外の すでに意味が定義されている関数 標準入力.h ヘッダー ファイルを使用して、ユーザーからの 1 つの入力を受け入れます。つまり、標準入力から 1 つの文字 (unsigned char) を取得するのは C ライブラリ関数です。ただし、getchar() 関数は getc() 関数に似ていますが、getchar() 関数と getc() 関数の間には小さな違いがあります。 C プログラミング言語 。 getchar() は標準入力から 1 文字を読み取り、getc() は任意の入力ストリームから 1 文字を読み取ります。

C の Getchar() 関数

構文

 int getchar (void); 

パラメータはありません。ただし、読み取った文字は int の unsigned char として返され、ファイルにエラーがある場合は、 終了後 ファイルの最後に。

次に、C で単一の文字を受け入れ、putchar () 関数を使用してそれらを出力する getchar() 関数プログラムをいくつか作成します。

ラドヤード・キプリングによる一行ごとの説明の場合

getchar() 関数を使用して 1 文字を読み取ります。

C の getchar() 関数を使用して単一を取るプログラムを考えてみましょう。

プログラム.c

 #include #include void main() { char c; printf ('
 Enter a character 
'); c = getchar(); // get a single character printf(' You have passed '); putchar(c); // print a single character using putchar getch(); } 

出力

 Enter a character A You have passed A 

上記のプログラムでわかるように、実行時に getchar() 関数を使用してユーザーから 1 文字を受け取ります。文字を取得した後、putchar() 関数を通じて文字を出力します。

getchar() 関数を使用してユーザーから n 文字を読み取ります

C の getchar() 関数を使用して n 文字を読み取るプログラムを考えてみましょう。

Javaの文字列形式

Getchar.c

 #include #include #include int main() { char ch; printf (' Enter a character ( If we want to exit press #) 
'); while (ch != '#') /* accept the number till the user does not enter the # to exit from the loop. */ { ch = getchar(); printf (' 
 We have entered the character : '); putchar (ch); // print a single character printf ('
'); } return 0; } 

出力

 Enter a character ( If we want to exit.. press #) A We have entered the character: A We have entered the character: B We have entered the character: B We have entered the character: C We have entered the character: C We have entered the character: 

上記の出力でわかるように、while ループは、ユーザーが # 文字を渡さなくなるまで、ユーザーからの文字を継続的に受け入れます。ここで、getchar() 関数は標準入力から 1 文字を取得し、それらを ch 変数に割り当てます。一方、putchar() 関数は読み取った文字を出力します。

scanf() 関数を使用して 1 文字を読み取る

C の scanf() ライブラリ関数を使用して文字を読み取るプログラムを考えてみましょう。

プログレッシブ

 #include #include int main() { char ch; printf ('
 Enter the character 
'); scanf ('%c', &ch); // get a single character, numeric or words printf( ' You have entered %c', ch); /* It print a single character or first letter of the words. */ return 0; } 

出力

すべて大文字のコマンド Excel
 Enter the character A You have entered A 

ご覧のとおり、上記のプログラムを実行すると、getchar() 関数の代わりに scanf() ライブラリ関数を使用して、単一の文字または文字のグループが取得されます。しかし、小さな違いがあります。 scanf() 関数はユーザーから 1 つの文字またはグループの文字を受け取ることができますが、getchar() 関数は 1 つの文字のみを受け取ることができます。

ここで再度上記のプログラムを実行すると、以下の結果が表示されます。

Javaの素数
 Enter the character Apple You have entered A 

do-while ループを使用して文字を読み取る

C の do while 関数と getchar() 関数を使用して文字を読み取るプログラムを考えてみましょう。

Dowhile1.c

 #include #include int main() { int ch, i = 0; char str[150]; printf (' Enter the characters from the keyboard (Press Enter button to stop).
'); // use do while loop to define the condition do { ch = getchar(); // takes character, number, etc from the user str[i] = ch; // store the ch into str[i] i++; // increment loop by 1 } while (ch != '
'); // ch is not equal to '
' printf('Entered characters are %s ', str); return 0; } 

出力

 Enter the characters from the keyboard (Press Enter button to stop). Well b47gvb come Entered characters are Well b47gvb come 

上記のプログラムでは、ユーザーがパラメータを渡すまで、do-while ループが継続的に文字を受け入れます。 入力 ボタンをクリックしてループを終了します。