-
putchar
鎖定
putcharputchar語法
(1)函數聲明
int putchar(int char)
(2)參數
- char-- 這是要被寫入的字符。該字符以其對應的 int 值進行傳遞。
(3)功能
(4)説明
該函數將指定的表達式的值所對應的字符輸出到標準輸出終端上。表達式可以是字符型或整型,它每次只能輸出一個字符。例如:“putchar('#')”輸出字符“#”。
putchar應用格式
putchar函數的基本格式為:putchar(c)。
(1)當c為一個被單引號(英文狀態下)引起來的字符時,輸出該字符(注:該字符也可為轉義字符);
(2)當c為一個介於0~127(包括0及127)之間的十進制整型數時,它會被視為對應字符的ASCII代碼,輸出該ASCII代碼對應的字符;
putchar注意事項
putchar函數返回值
(1)當輸出正確的時候,返回輸出字符轉換為的unsigned int 值;
(2)當輸出錯誤的時候,返回 EOF(End of file)文件結束符
if(putchar(c)==EOF) { printf("output error:%m\n"); exit(0); }
putchar程序示例
putchar示例1
#include <stdio.h> /* define some box-drawing characters */ #define LEFT_TOP 0xDA #define RIGHT_TOP 0xBF #define HORIZ 0xC4 #define VERT 0xB3 #define LEFT_BOT 0xC0 #define RIGHT_BOT 0xD9 int main(void) { char i, j; /* draw the top of the box */ putchar(LEFT_TOP); for(i=0; i<10; i++) { putchar(HORIZ); putchar(RIGHT_TOP); putchar('\n'); } /* draw the middle */ for(i=0; i<4; i++) putchar(VERT); for (j=0; j<10; j++) { putchar(' '); putchar(VERT); putchar('\n'); /* draw the bottom */ putchar(LEFT_BOT); } for(i=0; i<10; i++) { putchar(HORIZ); putchar(RIGHT_BOT); putchar('\n'); return 0; } }
putchar示例2
#include <stdio.h> int main() { char a,b,c; a='T';b='M';c='D'; putchar(a);putchar(b);putchar(c);putchar('\n'); putchar(a);putchar('\n'); putchar(b);putchar('\n'); putchar(c);putchar('\n'); return 0; }
輸出結果為: TMD T M D