putcputwc

将字符写入流。

语法

int putc(
   int c,
   FILE *stream
);
wint_t putwc(
   wchar_t c,
   FILE *stream
);

参数

c
要写入的字符。

stream
指向 FILE 结构的指针。

返回值

返回写入的字符。 若要指示错误或文件结尾条件,putcputchar 返回 EOFputwcputwchar 返回 WEOF。 对于所有四种例程,使用 ferrorfeof 检查是否存在错误或文件结尾。 如果向 stream 传递的是空指针,则调用无效参数处理程序,如参数验证中所述。 如果允许执行继续,则这些函数将返回 EOFWEOF,并将 errno 设置为 EINVAL

有关返回代码的详细信息,请参阅 errno_doserrno_sys_errlist_sys_nerr

注解

putc 例程会在当前位置将单个字符 c 写入输出 stream。 任何整数都可以传递给 putc,但是只写入低 8 位。 putchar 例程等同于 putc( c, stdout )。 对于每个例程,如果发生读取错误,则会设置流的错误指示器。 putcputchar 分别与 fputc_fputchar 类似,但是都作为函数和宏实现(请参阅在函数和宏之间选择的建议)。 putwcputwchar 分别是 putcputchar 的宽字符版本。 如果在 ANSI 模式下打开流,则 putwcputc 的行为相同。 putc 当前不支持到 UNICODE 流中的输出。

_nolock 后缀的版本是相同的,只是它们会受到其他线程的干扰。 有关详细信息,请参阅 _putc_nolock、_putwc_nolock

默认情况下,此函数的全局状态范围限定为应用程序。 若要更改此行为,请参阅 CRT 中的全局状态

一般文本例程映射

TCHAR.H 例程 _UNICODE_MBCS 未定义 _MBCS 已定义 _UNICODE 已定义
_puttc putc putc putwc

要求

例程 必需的标头
putc <stdio.h>
putwc <stdio.h> 或 <wchar.h>

通用 Windows 平台 (UWP) 应用中不支持控制台。 与控制台、stdinstdoutstderr 关联的标准流句柄必须重定向,然后 C 运行时函数才能在 UWP 应用中使用它们。 有关兼容性的详细信息,请参阅 兼容性

C 运行时库的所有版本。

示例

// crt_putc.c
/* This program uses putc to write buffer
* to a stream. If an error occurs, the program
* stops before writing the entire buffer.
*/

#include <stdio.h>

int main( void )
{
   FILE *stream;
   char *p, buffer[] = "This is the line of output\n";
   int  ch;

   ch = 0;
   /* Make standard out the stream and write to it. */
   stream = stdout;
   for( p = buffer; (ch != EOF) && (*p != '\0'); p++ )
      ch = putc( *p, stream );
}

输出

This is the line of output

另请参阅

流 I/O
fputcfputwc
getcgetwc