printf_s、_printf_s_l、wprintf_s、_wprintf_s_lprintf_s, _printf_s_l, wprintf_s, _wprintf_s_l

将格式化输出打印至标准输出流Prints formatted output to the standard output stream. 这些版本的 printf、_printf_l、wprintf、_wprintf_l 具有安全增强功能,如 CRT 中的安全功能所述。These versions of printf, _printf_l, wprintf, _wprintf_l have security enhancements, as described in Security Features in the CRT.


int printf_s(
   const char *format [,
int _printf_s_l(
   const char *format,
   locale_t locale [,
int wprintf_s(
   const wchar_t *format [,
int _wprintf_s_l(
   const wchar_t *format,
   locale_t locale [,


设置控件格式。Format control.

可选参数。Optional arguments.

要使用的区域设置。The locale to use.

返回值Return Value

返回输出的字符数或负值(如果出错)。Returns the number of characters printed, or a negative value if an error occurs.


Printf_s函数格式化并将一系列字符和值写入标准输出流stdoutThe printf_s function formats and prints a series of characters and values to the standard output stream, stdout. 如果自变量遵循格式字符串,格式字符串必须包含确定自变量的输出格式的规范。If arguments follow the format string, the format string must contain specifications that determine the output format for the arguments.

之间的主要区别printf_sprintf在于printf_s检查格式字符串中的有效格式设置字符,而printf仅检查格式字符串是否为 null 指针。The main difference between printf_s and printf is that printf_s checks the format string for valid formatting characters, whereas printf only checks if the format string is a null pointer. 如果任一检查失败,将调用无效参数处理程序,如参数验证中所述。If either check fails, an invalid parameter handler is invoked, as described in Parameter Validation. 如果允许执行继续,函数将返回-1 并设置errnoEINVALIf execution is allowed to continue, the function returns -1 and sets errno to EINVAL.

有关的信息errno和错误代码,请参阅_doserrno、 errno、 _sys_errlist 和 _sys_nerrFor information on errno and error codes, see _doserrno, errno, _sys_errlist, and _sys_nerr.

printf_sfprintf_s行为方式相同,只不过printf_s会将输出写入stdout而不是为目标的类型文件printf_s and fprintf_s behave identically except that printf_s writes output to stdout rather than to a destination of type FILE. 有关详细信息,请参阅 fprintf_s、_fprintf_s_l、fwprintf_s、_fwprintf_s_l.For more information, see fprintf_s, _fprintf_s_l, fwprintf_s, _fwprintf_s_l.

wprintf_s是宽字符版本printf_s;格式是宽字符字符串。wprintf_s is a wide-character version of printf_s; format is a wide-character string. wprintf_sprintf_s如果在 ANSI 模式下打开流,则行为相同。wprintf_s and printf_s behave identically if the stream is opened in ANSI mode. printf_s当前不到 UNICODE 流支持输出。printf_s doesn't currently support output into a UNICODE stream.

使用这些函数的版本 _l后缀完全相同,只不过它们使用传递中而不是当前线程区域设置的区域设置参数。The versions of these functions with the _l suffix are identical except that they use the locale parameter passed in instead of the current thread locale.

一般文本例程映射Generic-Text Routine Mappings

TCHAR.H 例程TCHAR.H routine 未定义 _UNICODE 和 _MBCS_UNICODE & _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _unicode_unicode defined
_tprintf_s_tprintf_s printf_sprintf_s printf_sprintf_s wprintf_swprintf_s
_tprintf_s_l_tprintf_s_l _printf_s_l_printf_s_l _printf_s_l_printf_s_l _wprintf_s_l_wprintf_s_l

格式参数包括普通字符,转义序列和 (如果自变量遵循格式) 格式规范。The format argument consists of ordinary characters, escape sequences, and (if arguments follow format) format specifications. 普通字符和转义序列复制到stdout按其出现的顺序。The ordinary characters and escape sequences are copied to stdout in order of their appearance. 例如,行For example, the line

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

生成输出produces the output

Line one
        Line two

格式规范始终以百分比符号开头 (%) 和左到右读取。Format specifications always begin with a percent sign (%) and are read left to right. printf_s遇到第一个格式规范 (如果有),它会将转换后的第一个参数的值格式并相应地将其输出。When printf_s encounters the first format specification (if any), it converts the value of the first argument after format and outputs it accordingly. 第二个格式规范致使第二个自变量转换并输出,依此类推。The second format specification causes the second argument to be converted and output, and so on. 如果存在比格式规范更多的自变量,则多出的自变量将被忽略。If there are more arguments than there are format specifications, the extra arguments are ignored. 如果全部格式规范没有足够自变量,则结果不确定。The results are undefined if there are not enough arguments for all the format specifications.


确保 format 不是用户定义的字符串。Ensure that format is not a user-defined string.


例程所返回的值Routine 必需的标头Required header
printf_s_printf_s_lprintf_s, _printf_s_l <stdio.h><stdio.h>
wprintf_s_wprintf_s_lwprintf_s, _wprintf_s_l <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.


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


示例输出Sample Output

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):
   Unicode                      Uni
Strings in field (2):
   Unicode                      Uni
Real numbers:
   251.736600 251.74 2.517366e+002 2.517366E+002

Address as:   0012FF78

请参阅See also

浮点支持Floating-Point Support
流 I/OStream I/O
fopen、_wfopen_wfopenfopen, _wfopen
fprintf、_fprintf_l、fwprintf、_fwprintf_lfprintf, _fprintf_l, fwprintf, _fwprintf_l
scanf、_scanf_l、wscanf、_wscanf_lscanf, _scanf_l, wscanf, _wscanf_l
sprintf、_sprintf_l、swprintf、_swprintf_l、__swprintf_lsprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l
vprintf 函数vprintf Functions