_vcprintf_s、_vcprintf_s_l、_vcwprintf_s、_vcwprintf_s_l_vcprintf_s, _vcprintf_s_l, _vcwprintf_s, _vcwprintf_s_l

使用指向参数列表的指针编写格式化输出到控制台。Writes formatted output to the console by using a pointer to a list of arguments. 这些版本的 _vcprintf、_vcprintf_l、_vcwprintf、_vcwprintf_l 具有安全增强功能,如 CRT 中的安全功能所述。These versions of _vcprintf, _vcprintf_l, _vcwprintf, _vcwprintf_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 _vcprintf(
   const char* format,
   va_list argptr
int _vcprintf(
   const char* format,
   locale_t locale,
   va_list argptr
int _vcwprintf_s(
   const wchar_t* format,
   va_list argptr
int _vcwprintf_s_l(
   const wchar_t* format,
   locale_t locale,
   va_list argptr


格式规范。Format specification.

指向参数列表的指针。Pointer to the list of arguments.

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

有关详细信息,请参阅格式规范语法:printf 和 wprintf 函数For more information, see Format Specification Syntax: printf and wprintf Functions.

返回值Return Value

写入的字符数,如果发生输出错误,则为一个负值。The number of characters written, or a negative value if an output error occurs.

与这些函数的不安全版本一样,如果format为空指针,则将调用无效参数处理程序,如参数验证中所述。Like the less secure versions of these functions, if format is a null pointer, the invalid parameter handler is invoked, as described in Parameter Validation. 此外,与这些函数的不太安全版本不同的是,如果格式不指定有效的格式,则会生成无效的参数异常。Additionally, unlike the less secure versions of these functions, if format does not specify a valid format, an invalid parameter exception is generated. 如果允许执行继续,则这些函数将返回错误代码并将errno设置为该错误代码。If execution is allowed to continue, these functions return an error code and set errno to that error code. 如果更具体的值不适用,则默认错误代码为EINVALThe default error code is EINVAL if a more specific value does not apply.


每个函数均采用一个指向参数列表的指针,然后将给定数据格式化并写入到控制台。Each of these functions takes a pointer to an argument list, and then formats and writes the given data to the console. _vcwprintf_s_vcprintf_s的宽字符版本。_vcwprintf_s is the wide-character version of _vcprintf_s. 它将采用一个宽字符字符串作为参数。It takes a wide-character string as an argument.

具有 _l后缀的这些函数的版本相同,只不过它们使用传入的区域设置参数而不是当前区域设置。The versions of these functions that have the _l suffix are identical except that they use the locale parameter that's passed in instead of the current locale.


确保 format不是用户定义的字符串。Ensure that format is not a user-defined string. 有关详细信息,请参阅 避免缓冲区溢出For more information, see Avoiding Buffer Overruns.

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

TCHAR.H 例程TCHAR.H routine 未定义 _UNICODE 和 _MBCS_UNICODE & _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_vtcprintf_s_vtcprintf_s _vcprintf_s_vcprintf_s _vcprintf_s_vcprintf_s _vcwprintf_s_vcwprintf_s
_vtcprintf_s_l_vtcprintf_s_l _vcprintf_s_l_vcprintf_s_l _vcprintf_s_l_vcprintf_s_l _vcwprintf_s_l_vcwprintf_s_l


例程所返回的值Routine 必需的标头Required header 可选标头Optional headers
_vcprintf_s_vcprintf_s_l_vcprintf_s, _vcprintf_s_l <conio.h> 和 <stdarg.h><conio.h> and <stdarg.h> <varargs.h>*<varargs.h>*
_vcwprintf_s_vcwprintf_s_l_vcwprintf_s, _vcwprintf_s_l <conio.h> 或 <wchar.h> 和 <stdarg.h><conio.h> or <wchar.h>, and <stdarg.h> <varargs.h>*<varargs.h>*

* 仅对 UNIX V 兼容性是必需的。* Required for UNIX V compatibility.

有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see Compatibility.


// crt_vcprintf_s.cpp
#include <conio.h>
#include <stdarg.h>

// An error formatting function used to print to the console.
int eprintf_s(const char* format, ...)
    va_list args;
    va_start(args, format);
    int result = _vcprintf_s(format, args);
    return result;

int main()
    eprintf_s("(%d:%d): Error %s%d : %s\n", 10, 23, "C", 2111,
              "<some error text>");
    eprintf_s("    (Related to symbol '%s' defined on line %d).\n",
              "<symbol>", 5 );
(10,23): Error C2111 : <some error text>
    (Related to symbol '<symbol>' defined on line 5).

请参阅See also

流 I/OStream I/O
vprintf 函数vprintf Functions
_cprintf、_cprintf_l、_cwprintf、_cwprintf_l_cprintf, _cprintf_l, _cwprintf, _cwprintf_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
va_arg、va_copy、va_end、va_startva_arg, va_copy, va_end, va_start