vprintf 函数vprintf Functions

每个 vprintf 函数均采用一个指向参数列表的指针,然后将给定数据格式化并写入到特定目标。Each of the vprintf functions takes a pointer to an argument list, then formats and writes the given data to a particular destination. 函数在执行参数验证过程中会有所不同,无论函数是采用宽字符字符串还是单字节字符串、是否具有输出目标以及是否支持指定格式字符串中使用的参数的顺序。The functions differ in the parameter validation performed, whether the functions take wide or single-byte character strings, the output destination, and the support for specifying the order in which parameters are used in the format string.

_vcprintf、_vcwprintf_vcprintf, _vcwprintf vfprintf、vfwprintfvfprintf, vfwprintf
_vfprintf_p、_vfprintf_p_l、_vfwprintf_p、_vfwprintf_p_l_vfprintf_p, _vfprintf_p_l, _vfwprintf_p, _vfwprintf_p_l vfprintf_s、_vfprintf_s_l、vfwprintf_s、_vfwprintf_s_lvfprintf_s, _vfprintf_s_l, vfwprintf_s, _vfwprintf_s_l
vprintf、vwprintfvprintf, vwprintf _vprintf_p、_vprintf_p_l、_vwprintf_p、_vwprintf_p_l_vprintf_p, _vprintf_p_l, _vwprintf_p, _vwprintf_p_l
vprintf_s、_vprintf_s_l、vwprintf_s、_vwprintf_s_lvprintf_s, _vprintf_s_l, vwprintf_s, _vwprintf_s_l vsprintf、vswprintfvsprintf, vswprintf
_vsprintf_p、_vsprintf_p_l、_vswprintf_p、_vswprintf_p_l_vsprintf_p, _vsprintf_p_l, _vswprintf_p, _vswprintf_p_l vsprintf_s、_vsprintf_s_l、vswprintf_s、_vswprintf_s_lvsprintf_s, _vsprintf_s_l, vswprintf_s, _vswprintf_s_l
_vscprintf、_vscprintf_l、_vscwprintf、_vscwprintf_l_vscprintf, _vscprintf_l, _vscwprintf, _vscwprintf_l _vsnprintf、_vsnwprintf_vsnprintf, _vsnwprintf

备注Remarks

vprintf 函数类似于下表中列出的其对应函数。The vprintf functions are similar to their counterpart functions as listed in the following table. 但是,每个 vprintf 函数接受一个指向参数列表的指针,而每个对应函数都接受一个参数列表。However, each vprintf function accepts a pointer to an argument list, whereas each of the counterpart functions accepts an argument list.

这些函数格式化用于输出到目标的数据,如下所示。These functions format data for output to destinations as follows.

函数Function 对应函数Counterpart function 输出目标Output destination 参数验证Parameter Validation 位置参数支持Positional Parameter Support
_vcprintf _cprintf_cprintf 控制台console 检查是否为 NULL。Check for null. no
_vcwprintf _cwprintf_cwprintf 控制台console 检查是否为 NULL。Check for null. no
vfprintf fprintffprintf Stream 检查是否为 NULL。Check for null. no
vfprintf_pvfprintf_p fprintf_pfprintf_p Stream 检查是否为 NULL 以及格式是否有效。Check for null and valid format. yes
vfprintf_s fprintf_sfprintf_s Stream 检查是否为 NULL 以及格式是否有效。Check for null and valid format. no
vfwprintf fwprintffwprintf Stream 检查是否为 NULL。Check for null. no
vfwprintf_pvfwprintf_p fwprintf_pfwprintf_p Stream 检查是否为 NULL 以及格式是否有效。Check for null and valid format. yes
vfwprintf_s fwprintf_sfwprintf_s Stream 检查是否为 NULL 以及格式是否有效。Check for null and valid format. no
vprintf printfprintf Stdout 检查是否为 NULL。Check for null. no
vprintf_pvprintf_p printf_pprintf_p Stdout 检查是否为 NULL 以及格式是否有效。Check for null and valid format. yes
vprintf_s printf_sprintf_s Stdout 检查是否为 NULL 以及格式是否有效。Check for null and valid format. no
vwprintf wprintfwprintf Stdout 检查是否为 NULL。Check for null. nono
vwprintf_pvwprintf_p wprintf_pwprintf_p Stdout 检查是否为 NULL 以及格式是否有效。Check for null and valid format. yes
vwprintf_s wprintf_swprintf_s Stdout 检查是否为 NULL 以及格式是否有效。Check for null and valid format. no
vsprintfvsprintf sprintfsprintf buffer 指向的内存memory pointed to by buffer 检查是否为 NULL。Check for null. no
vsprintf_pvsprintf_p sprintf_psprintf_p buffer 指向的内存memory pointed to by buffer 检查是否为 NULL 以及格式是否有效。Check for null and valid format. yes
vsprintf_s sprintf_ssprintf_s buffer 指向的内存memory pointed to by buffer 检查是否为 NULL 以及格式是否有效。Check for null and valid format. no
vswprintf swprintfswprintf buffer 指向的内存memory pointed to by buffer 检查是否为 NULL。Check for null. no
vswprintf_pvswprintf_p swprintf_pswprintf_p buffer 指向的内存memory pointed to by buffer 检查是否为 NULL 以及格式是否有效。Check for null and valid format. yes
vswprintf_s swprintf_sswprintf_s buffer 指向的内存memory pointed to by buffer 检查是否为 NULL 以及格式是否有效。Check for null and valid format. no
_vscprintf _vscprintf_vscprintf buffer 指向的内存memory pointed to by buffer 检查是否为 NULL。Check for null. no
_vscwprintf _vscwprintf_vscwprintf buffer 指向的内存memory pointed to by buffer 检查是否为 NULL。Check for null. no
_vsnprintf _snprintf_snprintf buffer 指向的内存memory pointed to by buffer 检查是否为 NULL。Check for null. no
_vsnwprintf _snwprintf_snwprintf buffer 指向的内存memory pointed to by buffer 检查是否为 NULL。Check for null. no

argptr 参数的类型为 va_list,它在 VARARGS.H 和 STDARG.H 中定义。The argptr argument has type va_list, which is defined in VARARGS.H and STDARG.H. argptr 变量必须由 va_start 初始化,并可由后续的 va_arg 调用重新初始化;然后 argptr 指向根据 format 参数中的相应规范转换和传输以用于输出的参数列表的开头。The argptr variable must be initialized by va_start, and may be reinitialized by subsequent va_arg calls; argptr then points to the beginning of a list of arguments that are converted and transmitted for output according to the corresponding specifications in the format argument. format 具有与 printf 的 format 参数相同的形式和函数。format has the same form and function as the format argument for printf. 这些函数均不会调用 va_endNone of these functions invokes va_end. 有关每个 vprintf 函数的更完整的说明,请参阅前面表中列出的其对应函数的说明。For a more complete description of each vprintf function, see the description of its counterpart function as listed in the preceding table.

_vsnprintf 与 vsprintf 的不同之处在于,前者写入 buffer 的内容不超过 count 字节。_vsnprintf differs from vsprintf in that it writes no more than count bytes to buffer.

名称中带有 w 中缀的这些函数的版本是不带 w 中缀的相应函数的宽字符版本;在其中每个宽字符函数中,buffer 和 format 是宽字符字符串。The versions of these functions with the w infix in the name are wide-character versions of the corresponding functions without the w infix; in each of these wide-character functions, buffer and format are wide-character strings. 否则,每个宽字符函数的行为会与其 SBCS 对应函数相同。Otherwise, each wide-character function behaves identically to its SBCS counterpart function.

具有 _s 和 _p 后缀的这些函数的版本是更安全的版本。The versions of these functions with _s and _p suffixes are the more secure versions. 这些版本验证格式字符串,如果格式字符串格式不正确(例如,使用无效的格式化字符的情况),这些版本会生成异常。These versions validate the format strings and will generate an exception if the format string is not well formed (for example, if invalid formatting characters are used).

具有 _p 后缀的这些函数的版本提供指定所提供的参数在格式字符串中替换的顺序的功能。The versions of these functions with the _p suffix provide the ability to specify the order in which the supplied arguments are substituted in the format string. 有关详细信息,请参阅 printf_p 位置参数For more information, see printf_p Positional Parameters.

对于 vsprintf、vswprintf_vsnprintf_vsnwprintf,如果在重叠的字符串之间发生复制,则此行为不确定。For vsprintf, vswprintf, _vsnprintf and _vsnwprintf, if copying occurs between strings that overlap, the behavior is undefined.

重要

确保 format 不是用户定义的字符串。Ensure that format is not a user-defined string. 有关详细信息,请参阅 避免缓冲区溢出For more information, see Avoiding Buffer Overruns. 如果使用这些函数的安全版本(_s 或 _p 后缀),如果用户提供的字符串包含无效的格式化字符,则用户提供的格式字符串可能会触发无效参数异常。If using the secure versions of these functions (either the _s or _p suffixes), a user-supplied format string could trigger an invalid parameter exception if the user-supplied string contains invalid formatting characters.

请参阅See Also

流 I/O Stream I/O
fprintf、_fprintf_l、fwprintf、_fwprintf_l fprintf, _fprintf_l, fwprintf, _fwprintf_l
printf、_printf_l、wprintf、_wprintf_l printf, _printf_l, wprintf, _wprintf_l
sprintf、_sprintf_l、swprintf、_swprintf_l、__swprintf_l sprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l
va_arg、va_copy、va_end、va_startva_arg, va_copy, va_end, va_start