_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 函数(_cwprintf_s_putwch)来输出字符。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. 每个 argument(如果有)根据 format 中相应的格式规范进行转换和输出。Each argument (if any) is converted and output according to the corresponding format specification in format. 格式具有与 printf_s 函数的 format 参数相同的形式和函数。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.

一个重要区别在于,在 Windows NT 中使用 _cwprintf_s 时,它将显示 Unicode 字符。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),如果 format 为 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 并将 errno 设置为 EINVALIn 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 _cprintf_s _cprintf_s _cwprintf_s
_tcprintf_s_l _cprintf_s_l _cprintf_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/O Console 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_l fprintf_s, _fprintf_s_l, fwprintf_s, _fwprintf_s_l
printf_s、_printf_s_l、wprintf_s、_wprintf_s_l printf_s, _printf_s_l, wprintf_s, _wprintf_s_l
sprintf_s、_sprintf_s_l、swprintf_s、_swprintf_s_l sprintf_s, _sprintf_s_l, swprintf_s, _swprintf_s_l
vfprintf_s、_vfprintf_s_l、vfwprintf_s、_vfwprintf_s_l vfprintf_s, _vfprintf_s_l, vfwprintf_s, _vfwprintf_s_l
格式规范语法:printf 和 wprintf 函数Format Specification Syntax: printf and wprintf Functions