函式
<cstdio>

getchar

int getchar ( void );
從標準輸入(stdin)獲取字元
返回來自標準輸入 (stdin) 的下一個字元。

它等價於以 stdin 作為引數呼叫 getc

引數

(無)

返回值

成功時,返回讀取到的字元(提升為int值)。
返回型別是int以容納特殊值 EOF,它表示失敗
如果標準輸入已到達檔案結尾,函式將返回 EOF 並設定 stdin檔案結尾指示符 (feof)。
如果發生其他讀取錯誤,函式也會返回 EOF,但會設定其錯誤指示符 (ferror)。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
/* getchar example : typewriter */
#include <stdio.h>

int main ()
{
  int c;
  puts ("Enter text. Include a dot ('.') in a sentence to exit:");
  do {
    c=getchar();
    putchar (c);
  } while (c != '.');
  return 0;
}

一個簡單的打字機。在按下回車鍵後,每個句子都會被回顯,直到文字中包含一個點號 (.)。

另見