putc ()-putchar ()- 写入字符
格式
#include <stdio.h>
int putc(int c, FILE *stream);
int putchar(int c);语言级别
ANSI
线程安全
False
#undef putc 或 #undef putchar 允许调用 putc 或 putchar 函数,而不是调用这些函数的宏版本。 这些函数是线程安全的。
描述
putc() 函数将 c 转换为 unsigned char ,然后在当前位置将 c 写入输出 stream 。 putchar() 等同于 putc(c, stdout)。
可以将 putc() 函数定义为宏,以便可以对自变量进行多次求值。
使用 type=record 打开的文件不支持 putc() 和 putchar() 函数。
返回值
putc() 和 putchar() 函数返回写入的字符。 返回值 EOF 指示错误。
errno 的值可以设置为:
- 值
- 含义
- ECONVERT
- 发生转换错误。
- EPUTANDGET
- 在读操作之后发生了非法写操作。
- EIOERROR
- 发生了不可恢复的I/O错误。
- EIORECERR
- 发生了可恢复的I/O错误。
示例
此示例将缓冲区的内容写入数据流。 在此示例中, for 语句的主体为空,因为该示例在测试表达式中执行写操作。
#include <stdio.h>
#include <string.h>
#define LENGTH 80
int main(void)
{
FILE *stream = stdout;
int i, ch;
char buffer[LENGTH + 1] = "Hello world";
/* This could be replaced by using the fwrite function */
for ( i = 0;
(i < strlen(buffer)) && ((ch = putc(buffer[i], stream)) != EOF);
++i);
}
/******************** Expected output: **************************
Hello world
*/