putc, putwc

將一個字元寫入資料流。

語法

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

參數

c
待寫入字元。

stream
FILE 結構的指標。

傳回值

傳回寫入的字元。 表示錯誤或檔案結尾條件, putcputchar 傳回 EOF ; putwcputwchar 傳回 WEOF 。 針對這四個常式,請使用 ferrorfeof 來檢查錯誤或檔案結尾。 如果 傳遞 的 stream Null 指標,則會叫用不正確參數處理常式,如參數驗證 中所述 。 如果允許繼續執行,這些函式會傳回 EOFWEOF ,並將 設定 errnoEINVAL

如需傳回碼的詳細資訊,請參閱 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
fputc, fputwc
getc, getwc