printf、_printf_l、wprintf、_wprintf_lprintf, _printf_l, wprintf, _wprintf_l

将格式化输出打印至标准输出流Prints formatted output to the standard output stream. 提供这些函数的更多安全版本,请参阅 printf_s、_printf_s_l、wprintf_s、_wprintf_s_lMore secure versions of these functions are available; see printf_s, _printf_s_l, wprintf_s, _wprintf_s_l.


int printf(
   const char *format [,
int _printf_l(
   const char *format,
   locale_t locale [,
int wprintf(
   const wchar_t *format [,
int _wprintf_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. 如果格式NULL,则调用无效参数处理程序,如中所述参数验证If format is NULL, the 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. 如果EOF (0xFFFF) 中遇到参数,该函数将返回-1。If EOF (0xFFFF) is encountered in argument, the function returns -1.

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


Printf函数格式和输出的字符和值写入标准输出流中,一系列stdoutThe printf 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. printffprintf行为方式相同,只不过printf将输出写入stdout而不是类型的目标文件.printf and fprintf behave identically except that printf writes output to stdout rather than to a destination of type FILE.

wprintf是宽字符版本的printf;格式是宽字符字符串。wprintf is a wide-character version of printf; format is a wide-character string. wprintfprintf如果在 ANSI 模式下打开流,则具有相同的行为。wprintf and printf behave identically if the stream is opened in ANSI mode. printf当前不支持输出到 UNICODE 流。printf does not 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_tprintf printfprintf printfprintf wprintfwprintf

格式参数包括普通字符,转义序列和 (如果自变量跟格式) 的格式规范。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("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遇到第一个格式规范 (如果有),它将转换后的第一个参数的值格式并相应地输出。When printf 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.

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

Tchar.h 例程Tchar.h routine 未定义 _UNICODE 和 _MBCS_UNICODE and _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_tprintf_tprintf printfprintf printfprintf wprintfwprintf
_tprintf_l_tprintf_l _printf_l_printf_l _printf_l_printf_l _wprintf_l_wprintf_l


例程Routine 必需的标头Required header
printf_printf_lprintf, _printf_l <stdio.h><stdio.h>
wprintf_wprintf_lwprintf, _wprintf_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.c
// This program uses the printf and wprintf functions
// to produce formatted output.

#include <stdio.h>

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

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

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

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

   // Display characters
   printf("Characters in field (1):\n"
          ch, ch, wch, wch);
   wprintf(L"Characters in field (2):\n"
           ch, ch, wch, wch);

   // Display strings
   printf("Strings in field (1):\n%25s\n"
          "%25.4hs\n   %S%25.3ls\n",
          string, string, wstring, wstring);
   wprintf(L"Strings in field (2):\n%25S\n"
           L"%25.4hs\n   %s%25.3ls\n",
           string, string, wstring, wstring);

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

   // Display pointer
   printf( "\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:   0012FF3C

请参阅See also

浮点支持Floating-Point Support
流 I/OStream I/O
fopen、_wfopen_wfopenfopen, _wfopen
_fprintf_p、_fprintf_p_l、_fwprintf_p、_fwprintf_p_l_fprintf_p, _fprintf_p_l, _fwprintf_p, _fwprintf_p_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