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 返回 EOFputwcputwchar 返回 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 传递的是空指针,则调用无效参数处理程序,如参数验证中所述。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 写入输出 streamThe 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 putc putc putwcputwc


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

通用 Windows 平台 (UWP) 应用中不支持控制台。The console is not supported in Universal Windows Platform (UWP) apps. 控制台中,与关联的标准流句柄stdinstdout,和stderr,必须将重定向,然后 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/O Stream I/O
fputc、fputwc fputc, fputwc
getc、getwcgetc, getwc