Share via


_cputs, _cputws

將字串放入主控台。

重要

這個 API 不能用於在 Windows 執行階段中執行的應用程式。 如需詳細資訊,請參閱 CRT functions not supported in Universal Windows Platform apps (通用 Windows 平台應用程式中不支援的 CRT 函式)。

語法

int _cputs(
   const char *str
);
int _cputws(
   const wchar_t *str
);

參數

str
輸出字串。

傳回值

如果成功,_cputs 會傳回 0。 如果函式失敗,則會傳回非零值。

備註

_cputs 函式會撰寫透過 str 直接指向主控台的以 null 終止的字串。 歸位字元換行字元 (CR-LF) 組合不會自動附加至字串。

這個函式會驗證其參數。 如果 strNULL ,則會叫用不正確參數處理常式,如參數驗證 中所述 。 如果允許繼續執行, errno 則會設定為 EINVAL ,並傳回 -1。

根據預設,此函式的全域狀態會限定于應用程式。 若要變更此行為,請參閱 CRT 中的全域狀態。

泛型文字常式對應

Tchar.h 常式 _UNICODE_MBCS 未定義 _MBCS 定義 _UNICODE 定義
_cputts _cputs _cputs _cputws

需求

常式 必要的標頭 選擇性標頭
_cputs <conio.h> <errno.h>
_cputws <conio.h> <errno.h>

如需相容性詳細資訊,請參閱相容性

程式庫

所有版本的 C 執行階段程式庫

範例

// crt_cputs.c
// compile with: /c
// This program first displays a string to the console.

#include <conio.h>
#include <errno.h>

void print_to_console(char* buffer)
{
   int retval;
   retval = _cputs( buffer );
   if (retval)
   {
       if (errno == EINVAL)
       {
         _cputs( "Invalid buffer in print_to_console.\r\n");
       }
       else
         _cputs( "Unexpected error in print_to_console.\r\n");
   }
}

void wprint_to_console(wchar_t* wbuffer)
{
   int retval;
   retval = _cputws( wbuffer );
   if (retval)
   {
       if (errno == EINVAL)
       {
         _cputws( L"Invalid buffer in wprint_to_console.\r\n");
       }
       else
         _cputws( L"Unexpected error in wprint_to_console.\r\n");
   }
}

int main()
{
   // String to print at console.
   // Notice the \r (return) character.
   char* buffer = "Hello world (courtesy of _cputs)!\r\n";
   wchar_t *wbuffer = L"Hello world (courtesy of _cputws)!\r\n";
   print_to_console(buffer);
   wprint_to_console( wbuffer );
}
Hello world (courtesy of _cputs)!
Hello world (courtesy of _cputws)!

另請參閱

主控台和埠 I/O
_putch, _putwch