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.

语法Syntax

int printf(  
   const char *format [,  
   argument]...   
);  
int _printf_l(  
   const char *format,  
   locale_t locale [,  
   argument]...   
);  
int wprintf(  
   const wchar_t *format [,  
   argument]...   
);  
int _wprintf_l(  
   const wchar_t *format,  
   locale_t locale [,  
   argument]...   
);  

参数Parameters

format
设置控件格式。Format control.

argument
可选参数。Optional arguments.

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

返回值Return Value

返回输出的字符数或负值(如果出错)。Returns the number of characters printed, or a negative value if an error occurs. 如果 formatNULL,则将调用无效的参数处理程序,如参数验证中所述。If format is NULL, the 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. 如果在 argument 中遇到 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.

备注Remarks

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

wprintfprintf 的宽字符版本;format 是宽字符字符串。wprintf is a wide-character version of printf; format is a wide-character string. 如果在 ANSI 模式下打开流,则 wprintfprintf 的行为相同。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 printf printf wprintf

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("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 遇到第一个格式规范(如果存在)时,它会在 format 后转换第一个自变量的值,并将其相应输出。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 printf printf wprintf
_tprintf_l _printf_l _printf_l _wprintf_l

惠?Requirements

例程所返回的值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.

示例Example

// 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"  
          "%10c%5hc%5C%5lc\n",  
          ch, ch, wch, wch);  
   wprintf(L"Characters in field (2):\n"  
           L"%10C%5hc%5c%5lc\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):  
                 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:   0012FF3C  

请参阅See Also

浮点支持 Floating-Point Support
流 I/O Stream I/O
区域设置 Locale
fopen、_wfopen fopen, _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_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
_set_output_format_set_output_format