putc、putwcputc, putwc

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

语法Syntax

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

参数Parameters

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

stream
指向文件结构的指针。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. 如果为传递 null 指针,则调用无效参数处理程序,如中所述参数验证If passed a null pointer for stream, the invalid parameter handler is invoked, as described in Parameter Validation. 如果允许执行继续,则这些函数将返回EOFWEOF并设置errnoEINVALIf 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.

备注Remarks

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. putwcputc如果在 ANSI 模式下打开流,则具有相同的行为。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

要求Requirements

例程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. 控制台中,与关联的标准流句柄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.

Libraries

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

示例Example

// 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 );
}

输出Output

This is the line of output

请参阅See also

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