getchar
提供: cppreference.com
ヘッダ <stdio.h> で定義
|
||
int getchar(void); |
||
stdin から次の文字を読み込みます。
目次 |
[編集] 引数
(なし)
[編集] 戻り値
成功した場合は取得した文字、失敗した場合は EOF。
失敗がファイル終端に達したことに起因する場合、さらに stdin のファイル終端指示子 (feof() を参照) をセットします。 失敗が何らかの他のエラーに起因する場合、 stdin のエラー指示子 (ferror() を参照) をセットします。
[編集] 例
getchar をエラーチェック付きで使用します。
Run this code
#include <stdio.h> #include <stdlib.h> int main(void) { int ch; while ((ch=getchar()) != EOF) /* read/print "abcde" from stdin */ printf("%c", ch); /* Test reason for reaching EOF. */ if (feof(stdin)) /* if failure caused by end-of-file condition */ puts("End of file reached"); else if (ferror(stdin)) /* if failure caused by some other error */ { perror("getchar()"); fprintf(stderr,"getchar() failed in file %s at line # %d\n", __FILE__,__LINE__-9); exit(EXIT_FAILURE); } return EXIT_SUCCESS; }
出力:
abcde End of file reached