Condividi tramite


_cprintf_s, _cprintf_s_l, _cwprintf_s, _cwprintf_s_l

Formats and prints to the console. These are versions of _cprintf, _cprintf_l, _cwprintf, _cwprintf_l with security enhancements as described in Security Features in the CRT.

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

Parameters

  • format
    Format-control string.

  • argument
    Optional parameters.

  • locale
    The locale to use.

Return Value

The number of characters printed.

Remarks

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. The format has the same form and function as the format parameter for the printf_s function. 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.

An important distinction is that _cwprintf_s displays Unicode characters when used in Windows NT. Unlike _cprintf_s, _cwprintf_s uses the current console locale

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.

Security noteSecurity Note

Ensure that format is not a user-defined string.

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. In all cases, If execution is allowed to continue, the functions return -1 and set errno to EINVAL.

Generic-Text Routine Mappings

Tchar.h routine

_UNICODE and _MBCS not defined

_MBCS defined

_UNICODE defined

_tcprintf_s

_cprintf_s

_cprintf_s

_cwprintf_s

_tcprintf_s_l

_cprintf_s_l

_cprintf_s_l

_cwprintf_s_l

Requirements

Routine

Required header

_cprintf_s, _cprintf_s_l

<conio.h>

_cwprintf_s, _cwprintf_s_l

<conio.h>

For more compatibility information, see Compatibility in the Introduction.

Libraries

All versions of the C run-time libraries.

Example

// 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 );
}

Output

-16  001d  62511  A Test

See Also

Reference

Console and Port I/O

_cscanf, _cscanf_l, _cwscanf, _cwscanf_l

fprintf_s, _fprintf_s_l, fwprintf_s, _fwprintf_s_l

printf_s, _printf_s_l, wprintf_s, _wprintf_s_l

sprintf_s, _sprintf_s_l, swprintf_s, _swprintf_s_l

vfprintf_s, _vfprintf_s_l, vfwprintf_s, _vfwprintf_s_l

Format Specification Fields: printf and wprintf Functions