// getchar_putchar.c
#include <stdio.h>
#include <stdlib.h>
int
main()
{
char cKey = '\0';
while (1) {
cKey = getchar();
if (cKey == 'Q' || cKey == 'q')
break;
else
putchar(cKey);
}
return 0;
}
相較於fgetc(), fputc()和getc(), putc(),getchar(), putchar()就單純地多了,因為它們就分別只能從標準輸入 (stdio)做讀取,例如鍵盤;和從標準輸出 (stdout)做輸出,例如螢幕。如同上述的程序碼,程式開始執行之后,使用者每從鍵盤輸入一個字元,螢幕就跟著輸出相同的字元,直到使用者輸入`Q' / `q' (離開程序)為止。在Linux C getc(), putc()的文章中有提到,getchar()和putchar()也是屬於函式呼叫;更細節地定義則是寫在/usr/include/bits/stdio.h:
// /usr/include/bits/stdio.h
/* Read a character from stdin. */
__STDIO_INLINE int getchar(void)
{
return _IO_getc(stdin);
}
/* Write a character to stdout. */
__STDIO_INLINE int putchar(int __c)
{
return _IO_putc(__c, stdout);
}
int getchar(void);int putchar(int c);
沒有留言:
張貼留言