_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.

语法Syntax

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

参数Parameters

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

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

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

返回值Return Value

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

备注Remarks

这些函数会格式化并打印一系列字符和值直接向控制台中,使用 _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. 如果格式是空指针,该函数将调用无效参数处理程序,如中所述参数验证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

要求Requirements

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

示例Example

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