_printf_p, _printf_p_l, _wprintf_p, _wprintf_p_l

將格式化的輸出列印至標準輸出資料流,並啟用在格式字串中使用參數的順序規格。

語法

int _printf_p(
   const char *format [,
   argument]...
);
int _printf_p_l(
   const char *format,
   _locale_t locale [,
   argument]...
);
int _wprintf_p(
   const wchar_t *format [,
   argument]...
);
int _wprintf_p_l(
   const wchar_t *format,
   _locale_t locale [,
   argument]...
);

參數

format
控制項格式。

argument
選擇性引數。

locale
要使用的地區設定。

傳回值

傳回列印的字元數;如果發生錯誤,則為負值。

備註

_printf_p 函式會格式化一連串的字元和值,並將其列印至標準輸出資料流 stdout。 如果引數接在 format 字串之後,format 字串必須包含決定引數輸出格式的規格 (請參閱 printf_p 位置參數)。

_printf_pprintf_s 之間的差異在於 _printf_p 支援位置參數,可讓您指定格式字串中使用引數的順序。 如需詳細資訊,請參閱 printf_p 位置參數

_wprintf_p 是寬字元版本的 _printf_p;如果資料流是以 ANSI 模式開啟,則這兩者的行為相同。 _printf_p 目前不支援輸出至 UNICODE 資料流。

這些有 _l 尾碼的函式版本是一樣的,不同之處在於會使用傳入的地區設定,而不使用目前的執行緒地區設定。

重要

確認 format 不是使用者定義的字串。

如果 formatargumentNULL ,或 的格式字串包含不正確格式字元, _printf_p_wprintf_p 函式會叫用不正確參數處理常式,如參數驗證 中所述 。 如果允許繼續執行,則函式會傳回 -1 並將 errno 設定為 EINVAL

泛型文字常式對應

Tchar.h 常式 _UNICODE_MBCS 未定義 _MBCS 定義 _UNICODE 定義
_tprintf_p _printf_p _printf_p _wprintf_p
_tprintf_p_l _printf_p_l _printf_p_l _wprintf_p_l

需求

常式 必要的標頭
_printf_p, _printf_p_l <stdio.h>
_wprintf_p, _wprintf_p_l <stdio.h > 或 < wchar.h>

通用 Windows 平臺 (UWP) 應用程式中不支援主控台。 與主控台、 stdinstdoutstderr 相關聯的標準資料流程控制碼必須先重新導向,C 執行時間函式才能在 UWP 應用程式中使用這些控制碼。 如需相容性詳細資訊,請參閱相容性

重要

從 Windows 10 版本 2004(組建 19041)開始,函 printf 式系列會根據 IEEE 754 規則來列印完全可代表的浮點數以進行四捨五入。 在舊版的 Windows 中,完全代表以 '5' 結尾的浮點數一律會四捨五入。 IEEE 754 指出,他們必須四捨五入到最接近的偶數數位(也稱為「銀行家的四捨五入」)。 例如, printf("%1.0f", 1.5) 和 應該 printf("%1.0f", 2.5) 四捨五入為 2。 先前,1.5 會四捨五入為 2,而 2.5 會四捨五入為 3。 這項變更只會影響確切可表示的數位。 例如,2.35 (在記憶體中表示時,其接近 2.3500000000000000008)會繼續四捨五入至 2.4。 這些函式完成的四捨五入現在也會遵守 所 fesetround 設定的浮點舍入模式。 之前,舍入一律選擇 FE_TONEAREST 行為。 這項變更只會影響使用 Visual Studio 2019 16.2 版和更新版本所建置的程式。 若要使用舊版浮點舍入行為,請與 legacy_stdio_float_rounding.obj 連結。

範例

// crt_printf_p.c
// This program uses the _printf_p and _wprintf_p
// functions to choose the order in which parameters
// are used.

#include <stdio.h>

int main( void )
{
   // Positional arguments
   _printf_p( "Specifying the order: %2$s %3$s %1$s %4$s %5$s.\n",
              "little", "I'm", "a", "tea", "pot");

   // Resume arguments
   _wprintf_p( L"Reusing arguments: %1$d %1$d %1$d %1$d\n", 10);

   // Width argument
   _printf_p("Width specifiers: %1$*2$s", "Hello\n", 10);
}
Specifying the order: I'm a little tea pot.
Reusing arguments: 10 10 10 10
Width specifiers:     Hello

另請參閱

數學和浮點支援
資料流 I/O
地區設定
fopen, _wfopen
_fprintf_p, _fprintf_p_l, _fwprintf_p, _fwprintf_p_l
fprintf, _fprintf_l, fwprintf, _fwprintf_l
fprintf_s, _fprintf_s_l, fwprintf_s, _fwprintf_s_l
scanf, _scanf_l, wscanf, _wscanf_l
scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l
_sprintf_p, _sprintf_p_l, _swprintf_p, _swprintf_p_l
sprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l
sprintf_s, _sprintf_s_l, swprintf_s, _swprintf_s_l
vprintf 函數