putchar

putchar

在头文件中定义
int putchar(int ch);

将一个字符写入chstdout。在内部,字符unsigned char在被写入之前被转换。

Equivalent to putc(ch, stdout).

参数

ch-character to be written

返回值

成功时,返回书面字符。

失败时,返回EOF并设置错误指示器(请参阅参考资料ferror()stdout

带错误检查的putchar。

#include <stdio.h> #include <stdlib.h> int main(void) { int ret_code = 0; for (char c = 'a'; (ret_code != EOF) && (c != 'z' c++) ret_code = putchar(c /* Test whether EOF was reached. */ if (ret_code == EOF) if (ferror(stdout)) { fprintf(stderr,"putchar() failed in file %s at line # %d\n", __FILE__,__LINE__-6 perror("putchar()" exit(EXIT_FAILURE } putchar('\n' // putchar return value is not equal to the argument int r = 0x1070; printf("\n0x%x\n", r r = putchar(r printf("\n0x%x\n", r }

输出:

abcdefghijklmnopqrstuvwxy 0x1070 p 0x70

参考

  • C11标准(ISO / IEC 9899:2011):