非常教程

C参考手册

文件输入/输出 | File input/output

putc

在头文件<stdio.h>中定义

int fputc(int ch,FILE * stream);

int putc(int ch,FILE * stream);

将字符写入ch给定的输出流streamputc()可能被实现为一个宏并且stream不止一次地进行评估,所以相应的参数不应该是带有副作用的表达式。

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

参数

CH

-

字符被写入

-

输出流

Return value

成功时,返回书面字符。

On failure, returns EOF and sets the error indicator (see ferror()) on stream.

带有错误检查的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);
 
    /* Test whether EOF was reached. */
    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 fputc函数(p:331)
    • 7.21.7.7 putc函数(p:333)
  • C99标准(ISO / IEC 9899:1999):
    • 7.19.7.3 fputc函数(p:297)
    • 7.19.7.8 putc函数(p:299)
  • C89 / C90标准(ISO / IEC 9899:1990):
    • 4.9.7.3 fputc函数
    • 4.9.7.8 putc函数
C

C 语言是一门通用计算机编程语言,应用广泛。C 语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。