fputc, putc
来自cppreference.com
                    
                                        
                    
                    
                                                            
                    | 在标头  <stdio.h>定义 | ||
| int fputc( int ch, FILE *stream ); | ||
| int putc( int ch, FILE *stream ); | ||
写入字符 ch 到给定输出流 stream 。 putc() 可以实现为宏并对 stream 求值超过一次,故对应的参数决不应是有副效应的表达式。
在内部,在写入前将字符转换为 unsigned char 。
参数
| ch | - | 要被写入的字符 | 
| stream | - | 输出流 | 
参数
| ch | - | 要写入的字符 | 
| stream | - | 输出流 | 
返回值
成功时,返回被写入字符。
失败时,返回 EOF 并设置 stream 上的错误指示器(见 ferror() )。
示例
putc带错误检查
运行此代码
#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 = putc(c, stdout); /* 测试是否抵达 EOF 。 */ if (ret_code == EOF) if (ferror(stdout)) { perror("putc()"); fprintf(stderr,"putc() failed in file %s at line # %d\n", __FILE__,__LINE__-7); exit(EXIT_FAILURE); } putc('\n', stdout); return EXIT_SUCCESS; }
输出:
abcdefghijklmnopqrstuvwxy
引用
- C11 标准(ISO/IEC 9899:2011):
- 7.21.7.3 The fputc function (第 331 页)
 
- 7.21.7.7 The putc function (第 333 页)
 
- C99 标准(ISO/IEC 9899:1999):
- 7.19.7.3 The fputc function (第 297 页)
 
- 7.19.7.8 The putc function (第 299 页)
 
- C89/C90 标准(ISO/IEC 9899:1990):
- 4.9.7.3 The fputc function
 
- 4.9.7.8 The putc function
 
参阅
| 将一个字符写入 stdout(函数) |