putc、putwcputc, putwc

将字符写入流。Writes a character to a stream.


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


要写入的字符。Character to be written.

指向文件结构的指针。Pointer to FILE structure.

返回值Return Value

返回写入的字符。Returns the character written. 若要指示错误或文件尾条件, putcputchar返回EOF;putwcputwchar返回WEOFTo indicate an error or end-of-file condition, putc and putchar return EOF; putwc and putwchar return WEOF. 对于所有四种例程,使用 ferrorfeof 来检查是否存在错误或文件结尾。For all four routines, use ferror or feof to check for an error or end of file. 如果为stream传递了 null 指针,则将调用无效的参数处理程序,如参数验证中所述。If passed a null pointer for stream, the invalid parameter handler is invoked, as described in Parameter Validation. 如果允许执行继续,则这些函数将返回EOFWEOF ,并将errno设置为EINVALIf execution is allowed to continue, these functions return EOF or WEOF and set errno to EINVAL.

有关这些代码以及其他错误代码的详细信息,请参阅 _doserrno、errno、_sys_errlist 和 _sys_nerrSee _doserrno, errno, _sys_errlist, and _sys_nerr for more information on these, and other, error codes.


Putc例程将单个字符c写入当前位置的输出中。The putc routine writes the single character c to the output stream at the current position. 任何整数都可以传递到putc,但仅写入较低的8位。Any integer can be passed to putc, but only the lower 8 bits are written. Putchar例程与完全相同putc( c, stdout )The putchar routine is identical to putc( c, stdout ). 对于每个例程,如果发生读取错误,则会设置流的错误指示器。For each routine, if a read error occurs, the error indicator for the stream is set. putcputchar分别与fputc_fputchar相似,但同时作为函数和宏实现(请参阅在函数和宏之间选择)。putc and putchar are similar to fputc and _fputchar, respectively, but are implemented both as functions and as macros (see Choosing Between Functions and Macros). putwcputwchar分别是putcputchar的宽字符版本。putwc and putwchar are wide-character versions of putc and putchar, respectively. 如果在 ANSI 模式下打开流,则putwcputc的行为相同。putwc and putc behave identically if the stream is opened in ANSI mode. putc当前不支持输出到 UNICODE 流中。putc doesn't currently support output into a UNICODE stream.

后缀为 _nolock 的版本是相同的,只不过它们可能会受到其他线程的影响。The versions with the _nolock suffix are identical except that they are not protected from interference by other threads. 有关详细信息,请参阅 _putc_nolock、_putwc_nolockFor more information, see _putc_nolock, _putwc_nolock.

一般文本例程映射Generic-Text Routine Mappings

TCHAR.H 例程TCHAR.H routine 未定义 _UNICODE 和 _MBCS_UNICODE & _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_puttc_puttc putcputc putcputc putwcputwc


例程所返回的值Routine 必需的标头Required header
putcputc <stdio.h><stdio.h>
putwcputwc <stdio.h> 或 <wchar.h><stdio.h> or <wchar.h>

通用 Windows 平台 (UWP) 应用中不支持控制台。The console is not supported in Universal Windows Platform (UWP) apps. 与控制台、 stdinstdoutstderr关联的标准流句柄必须重定向, 然后 C 运行时函数才能在 UWP 应用中使用它们。The standard stream handles that are associated with the console, stdin, stdout, and stderr, must be redirected before C run-time functions can use them in UWP apps. 有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see Compatibility.


C 运行时库的所有版本。All versions of the C run-time libraries.


// 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

请参阅See also

流 I/OStream I/O
fputc、fputwcfputc, fputwc
getc、getwcgetc, getwc