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. 如果自变量跟在 format 字符串后面,则 format 字符串必须包含确定自变量的输出格式的规范。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 并将 errno 设置为 EINVALIf 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而不是类型的目标FILEprintf_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_sprintf_s 的宽字符版本;format 是宽字符字符串。wprintf_s is a wide-character version of printf_s; format is a wide-character string. 如果在 ANSI 模式下打开流,则 wprintf_sprintf_s 的行为相同。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 printf_s printf_s wprintf_s
_tprintf_s_l _printf_s_l _printf_s_l _wprintf_s_l

format 自变量由普通字符、转义序列和(如果自变量遵循 format)格式规范组成。The format argument consists of ordinary characters, escape sequences, and (if arguments follow format) format specifications. 将普通字符和转义序列按照其外观的顺序复制到 stdoutThe 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 遇到第一个格式规范(如果存在)时,它会在 format 后转换第一个自变量的值,并将其相应输出。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/O Stream I/O
区域设置 Locale
fopen、_wfopen fopen, _wfopen
fprintf、_fprintf_l、fwprintf、_fwprintf_l fprintf, _fprintf_l, fwprintf, _fwprintf_l
scanf、_scanf_l、wscanf、_wscanf_l scanf, _scanf_l, wscanf, _wscanf_l
sprintf、_sprintf_l、swprintf、_swprintf_l、__swprintf_l sprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l
vprintf 函数vprintf Functions