_cprintf_s、_cprintf_s_l、_cwprintf_s、_cwprintf_s_l_cprintf_s, _cprintf_s_l, _cwprintf_s, _cwprintf_s_l

格式化并打印到控制台。Formats and prints to the console. 这些版本的 _cprintf、_cprintf_l、_cwprintf、_cwprintf_l 具有安全增强功能,如 CRT 中的安全功能中所述。These versions of _cprintf, _cprintf_l, _cwprintf, _cwprintf_l have security enhancements, as described in Security Features in the CRT.


此 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_s(
   const char * format [,
   argument] ...
int _cprintf_s_l(
   const char * format,
   locale_t locale [,
   argument] ...
int _cwprintf_s(
   const wchar * format [,
   argument] ...
int _cwprintf_s_l(
   const wchar * format,
   locale_t locale [,
   argument] ...


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

可选参数。Optional parameters.

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

返回值Return Value

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


这些函数会格式化并打印一系列字符和值直接到控制台,使用 _putch函数 (_putwch_cwprintf_s) 到输出字符。These functions format and print a series of characters and values directly to the console, using the _putch function (_putwch for _cwprintf_s) to output characters. 每个参数(如果有) 进行转换和输出中的相应格式规范根据格式Each argument (if any) is converted and output according to the corresponding format specification in format. 该格式具有相同形式和函数与格式参数printf_s函数。The format has the same form and function as the format parameter for the printf_s function. 与不同fprintf_sprintf_s,和sprintf_s函数,两者 _cprintf_s也不 _cwprintf_s会换行字符将转换为回车换行符 (CR-LF) 组合输出时。Unlike the fprintf_s, printf_s, and sprintf_s functions, neither _cprintf_s nor _cwprintf_s translates line-feed characters into carriage return-line feed (CR-LF) combinations when output.

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

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


确保 format 不是用户定义的字符串。Ensure that format is not a user-defined string.

如不安全版本 (请参阅_cprintf、 _cprintf_l、 _cwprintf、 _cwprintf_l),这些函数验证其参数和中所述将调用无效参数处理程序,参数验证,如果格式是 null 指针。Like the non-secure versions (see _cprintf, _cprintf_l, _cwprintf, _cwprintf_l), these functions validate their parameters and invoke the invalid parameter handler, as described in Parameter Validation, if format is a null pointer. 这些函数与不安全版本的不同之处在于,格式字符串本身也需进行验证。These functions differ from the non-secure versions in that the format string itself is also validated. 如果有任何未知或格式错误的格式化说明符,则这些函数将调用无效参数处理程序。If there are any unknown or badly formed formatting specifiers, these functions invoke the invalid parameter handler. 在所有情况下,如果允许执行继续,则函数返回-1 并设置errnoEINVALIn all cases, If execution is allowed to continue, the functions return -1 and set errno to EINVAL.

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

Tchar.h 例程Tchar.h routine 未定义 _UNICODE 和 _MBCS_UNICODE and _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_tcprintf_s_tcprintf_s _cprintf_s_cprintf_s _cprintf_s_cprintf_s _cwprintf_s_cwprintf_s
_tcprintf_s_l_tcprintf_s_l _cprintf_s_l_cprintf_s_l _cprintf_s_l_cprintf_s_l _cwprintf_s_l_cwprintf_s_l


例程Routine 必需的标头Required header
_cprintf_s_cprintf_s_l_cprintf_s, _cprintf_s_l <conio.h><conio.h>
_cwprintf_s_cwprintf_s_l_cwprintf_s, _cwprintf_s_l <conio.h><conio.h>

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


C 运行时库的所有版本。All versions of the C run-time libraries.


// crt_cprintf_s.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_s( "%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_s、_fprintf_s_l、fwprintf_s、_fwprintf_s_lfprintf_s, _fprintf_s_l, fwprintf_s, _fwprintf_s_l
printf_s、_printf_s_l、wprintf_s、_wprintf_s_lprintf_s, _printf_s_l, wprintf_s, _wprintf_s_l
sprintf_s、_sprintf_s_l、swprintf_s、_swprintf_s_lsprintf_s, _sprintf_s_l, swprintf_s, _swprintf_s_l
vfprintf_s、_vfprintf_s_l、vfwprintf_s、_vfwprintf_s_lvfprintf_s, _vfprintf_s_l, vfwprintf_s, _vfwprintf_s_l
格式规范语法:printf 和 wprintf 函数Format Specification Syntax: printf and wprintf Functions