Share via


printf_s、_printf_s_l、wprintf_s、_wprintf_s_l

将格式化输出打印至标准输出流。 printf、_printf_l、wprintf、_wprintf_l 的这些版本如 CRT 中的安全功能 所述,其安全得到了增强。

int printf_s(
   const char *format [,
   argument]... 
);
int _printf_s_l(
   const char *format,
   locale_t locale [,
   argument]... 
);
int wprintf_s(
   const wchar_t *format [,
   argument]... 
);
int _wprintf_s_l(
   const wchar_t *format,
   locale_t locale [,
   argument]... 
);

参数

  • format
    格式控件。

  • argument
    可选参数。

  • locale
    要使用的区域设置。

返回值

返回打印的字符数,或在发生错误时返回负值。

备注

printf_s 函数对一系列字符和值设置格式并将其打印到标准输出流 stdout 中。 如果参数紧跟 格式 字符串,format 字符串必须包含确定参数输出格式的规范。

printf_s 和 printf 之间的一个主要区别是,printf_s 检查格式字符串中的有效格式设置字符,而 printf 仅检查格式字符串是否为空指针。 如果任一检查失败,将调用无效参数处理程序(在参数验证所述)。 如果允许执行继续,则该函数返回 -1 并将 errno 设置为 EINVAL。

有关 errno 和错误代码的信息,请参见 _doserrno、errno、_sys_errlist 和 _sys_nerr

printf_s和fprintf_s具有相同的行为,除了printf_s输出到stdout而不输出到FILE类型。 有关详细信息,请参阅fprintf_s、_fprintf_s_l、fwprintf_s、_fwprintf_s_l

wprintf_s 是 printf_s 的宽字符版本;format 是宽字符串。 如果流在 ANSI 模式中打开,wprintf_s 和 printf_s 会具有相同的行为。 printf_s 当前不支持输出到 UNICODE 流。

这些带有 _l 后缀的函数的版本相同,只不过它们使用传递的区域设置参数而不是当前线程区域设置。

一般文本例程映射

TCHAR.H 例程

未定义 _UNICODE & _MBCS

已定义 _MBCS

已定义 _UNICODE

_tprintf_s

printf_s

printf_s

wprintf_s

_tprintf_s_l

_printf_s_l

_printf_s_l

_wprintf_s_l

format 参数包括普通字符、转义序列和(如果参数采用 format)格式规范。 遵循其外观的序列将普通字符和转义序列复制到 stdout。 例如,行

printf_s("Line one\n\t\tLine two\n"); 

生成输出

Line one
        Line two

格式规范始终以百分号 (%) 开头并从左向右读取。 当 printf_s 遇到第一个格式规范(如果有)时,它会转换 format 后第一个参数的值并相应地将其输出。 第二种格式规范会导致将第二个参数转换并输出,等等。 如果具有比格式规范更多的参数,则忽略额外的参数。 如果所有的格式规范都没有足够的参数,则结果为未定义。

安全说明安全说明

确保 format 不是用户定义的字符串。

要求

例程

必需的标头

printf_s, _printf_s_l

<stdio.h>

wprintf_s, _wprintf_s_l

<stdio.h> 或 <wchar.h>

控制台在 Windows 应用商店 应用程序中不受支持。 与控制台 stdin、stdout 和 stderr 关联的标准流句柄必须重定向,然后 C 运行时函数才可以在 Windows 应用商店 应用程序中使用它们。 有关兼容性的更多信息,请参见兼容性

示例

// crt_printf_s.c
/* This program uses the printf_s and wprintf_s functions
 * to produce formatted output.
 */

#include <stdio.h>

int main( void )
{
   char   ch = 'h', *string = "computer";
   int    count = -9234;
   double fp = 251.7366;
   wchar_t wch = L'w', *wstring = L"Unicode";

   /* Display integers. */
   printf_s( "Integer formats:\n"
           "   Decimal: %d  Justified: %.6d  Unsigned: %u\n",
           count, count, count );

   printf_s( "Decimal %d as:\n   Hex: %Xh  C hex: 0x%x  Octal: %o\n",
            count, count, count, count );

   /* Display in different radixes. */
   printf_s( "Digits 10 equal:\n   Hex: %i  Octal: %i  Decimal: %i\n",
            0x10, 010, 10 );

   /* Display characters. */

   printf_s("Characters in field (1):\n%10c%5hc%5C%5lc\n", ch, ch, wch, wch);
   wprintf_s(L"Characters in field (2):\n%10C%5hc%5c%5lc\n", ch, ch, wch, wch);

   /* Display strings. */

   printf_s("Strings in field (1):\n%25s\n%25.4hs\n   %S%25.3ls\n",
   string, string, wstring, wstring);
   wprintf_s(L"Strings in field (2):\n%25S\n%25.4hs\n   %s%25.3ls\n",
       string, string, wstring, wstring);

   /* Display real numbers. */
   printf_s( "Real numbers:\n   %f %.2f %e %E\n", fp, fp, fp, fp );

   /* Display pointer. */
   printf_s( "\nAddress as:   %p\n", &count);

}

示例输出

Integer formats:
   Decimal: -9234  Justified: -009234  Unsigned: 4294958062
Decimal -9234 as:
   Hex: FFFFDBEEh  C hex: 0xffffdbee  Octal: 37777755756
Digits 10 equal:
   Hex: 16  Octal: 8  Decimal: 10
Characters in field (1):
         h    h    w    w
Characters in field (2):
         h    h    w    w
Strings in field (1):
                 computer
                     comp
   Unicode                      Uni
Strings in field (2):
                 computer
                     comp
   Unicode                      Uni
Real numbers:
   251.736600 251.74 2.517366e+002 2.517366E+002

Address as:   0012FF78

.NET Framework 等效项

请参见

参考

浮点支持

流 I/O

区域设置

fopen、_wfopen

fprintf、_fprintf_l、fwprintf、_fwprintf_l

scanf、_scanf_l、wscanf、_wscanf_l

sprintf、_sprintf_l、swprintf、_swprintf_l、__swprintf_l

vprintf 函数