_cprintf、_cprintf_l、_cwprintf、_cwprintf_l_cprintf, _cprintf_l, _cwprintf, _cwprintf_l

格式化并打印到控制台。Formats and prints to the console. 提供更为安全的版本;请参阅 _cprintf_s、_cprintf_s_l、_cwprintf_s、_cwprintf_s_lMore-secure versions are available; see _cprintf_s, _cprintf_s_l, _cwprintf_s, _cwprintf_s_l.


此 API 不能用于在 Windows 运行时中执行的应用程序。This API cannot be used in applications that execute in the Windows Runtime. 有关详细信息,请参阅通用 Windows 平台应用中不支持的 CRT 函数For more information, see CRT functions not supported in Universal Windows Platform apps.


int _cprintf(
   const char * format [, argument_list]
int _cprintf_l(
   const char * format,
   locale_t locale [, argument_list]
int _cwprintf(
   const wchar * format [, argument_list]
int _cwprintf_l(
   const wchar * format,
   locale_t locale [, argument_list]


窗体控件字符串。Format-control string.

格式字符串的可选参数。Optional parameters for the format string.

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

返回值Return Value

打印的字符数。The number of characters printed.


这些函数会格式化并打印一系列字符和值直接到控制台,使用 _putch函数 (_putwch_cwprintf) 为输出字符.These functions format and print a series of characters and values directly to the console, using the _putch function (_putwch for _cwprintf) to output characters. 在每个自变量argument_list (如果有) 进行转换和输出中的相应格式规范根据格式Each argument in argument_list (if any) is converted and output according to the corresponding format specification in format. 格式自变量使用格式规范语法 printf 和 wprintf 函数The format argument uses the format specification syntax for printf and wprintf functions. 与不同fprintfprintf,和sprintf函数,两者 _cprintf也不 _cwprintf会换行字符将转换为回车换行符 (CR-LF) 组合输出时。Unlike the fprintf, printf, and sprintf functions, neither _cprintf nor _cwprintf translates line-feed characters into carriage return-line feed (CR-LF) combinations when output.

一个重要区别在于, _cwprintf显示 Unicode 字符在 Windows 中使用时。An important distinction is that _cwprintf displays Unicode characters when used in Windows. 与不同 _cprintf_cwprintf使用当前控制台区域设置。Unlike _cprintf, _cwprintf uses the current console locale settings.

使用这些函数的版本 _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 locale.

_cprintf验证格式参数。_cprintf validates the format parameter. 如果格式是 null 指针,函数将调用无效参数处理程序,如中所述参数验证If format is a null pointer, the function invokes the invalid parameter handler, as described in Parameter Validation. 如果允许执行继续,函数将返回-1 并设置errnoEINVALIf execution is allowed to continue, the function returns -1 and sets errno to EINVAL.


确保 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
_tcprintf_tcprintf _cprintf_cprintf _cprintf_cprintf _cwprintf_cwprintf
_tcprintf_l_tcprintf_l _cprintf_l_cprintf_l _cprintf_l_cprintf_l _cwprintf_l_cwprintf_l


例程Routine 必需的标头Required header
_cprintf_cprintf_l_cprintf, _cprintf_l <conio.h><conio.h>
_cwprintf_cwprintf_l_cwprintf, _cwprintf_l <conio.h><conio.h>

有关更多兼容性信息,请参阅 兼容性For more compatibility information, see Compatibility.


// crt_cprintf.c
// compile with: /c
// This program displays some variables to the console.

#include <conio.h>

int main( void )
    int         i = -16,
                h = 29;
    unsigned    u = 62511;
    char        c = 'A';
    char        s[] = "Test";

    // Note that console output does not translate \n as
    // standard output does. Use \r\n instead.
    _cprintf( "%d  %.4x  %u  %c %s\r\n", i, h, u, c, s );
-16  001d  62511  A Test

请参阅See also

控制台和端口 I/OConsole and Port I/O
_cscanf、_cscanf_l、_cwscanf、_cwscanf_l_cscanf, _cscanf_l, _cwscanf, _cwscanf_l
fprintf、_fprintf_l、fwprintf、_fwprintf_lfprintf, _fprintf_l, fwprintf, _fwprintf_l
printf、_printf_l、wprintf、_wprintf_lprintf, _printf_l, wprintf, _wprintf_l
sprintf、_sprintf_l、swprintf、_swprintf_l、__swprintf_lsprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l
vfprintf、_vfprintf_l、vfwprintf、_vfwprintf_lvfprintf, _vfprintf_l, vfwprintf, _vfwprintf_l
_cprintf_s、_cprintf_s_l、_cwprintf_s、_cwprintf_s_l_cprintf_s, _cprintf_s_l, _cwprintf_s, _cwprintf_s_l
_cprintf_p、_cprintf_p_l、_cwprintf_p、_cwprintf_p_l_cprintf_p, _cprintf_p_l, _cwprintf_p, _cwprintf_p_l
格式规范语法:printf 和 wprintf 函数Format Specification Syntax: printf and wprintf Functions