C Library Function - vprintf()
Description
C library function *int vprintf(const char format, va_list arg) Sends formatted output to standard output stdout using an argument list.
Declaration
Below is the declaration of the vprintf() function.
int vprintf(const char *format, va_list arg)
Parameters
- format -- This is a string containing the text to be written to standard output stdout. It can contain embedded format tags, which can be replaced by values specified in subsequent additional arguments and formatted as required. The format tag attribute is ** %[flags][width][.precision][length]specifier**, explained in detail below:
| specifier (specifier) | Output |
|---|---|
| c | Character |
| d or i | Signed decimal integer |
| e | Scientific notation using the e character (mantissa and exponent) |
| E | Scientific notation using the E character (mantissa and exponent) |
| f | Decimal floating-point number |
| g | Automatically select the appropriate representation between %e or %f |
| G | Automatically select the appropriate representation between %E or %f |
| o | Signed octal |
| s | String of characters |
| u | Unsigned decimal integer |
| x | Unsigned hexadecimal integer |
| X | Unsigned hexadecimal integer (uppercase letters) |
| p | Pointer address |
| n | No output |
| % | Character |
| flags | Description |
|---|---|
| - | Left-justify within the given field width; right alignment is the default (see the width sub-specifier). |
| + | Force a plus or minus sign (+ or -) to appear before the result; that is, a + sign is shown before positive numbers. By default, only negative numbers are preceded by a - sign. |
| (space) | If no sign is to be written, a space is inserted before the value. |
| # | When used with o, x, or X specifiers, a nonzero value is prefixed with 0, 0x, or 0X, respectively. |
与 e、E 和 f 一起使用时,会强制输出包含一个小数点,即使后边没有数字时也会显示小数点。默认情况下,如果后边没有数字时候,不会显示显示小数点。
与 g 或 G 一起使用时,结果与使用 e 或 E 时相同,但是尾部的零不会被移除。 |
| 0 | Place zeros (0) to the left of the number for padding instead of spaces (see the width sub-specifier). |
| width (width) | Description |
|---|---|
| (number) | The minimum number of characters to be output. If the value to be output is shorter than this number, the result is padded with spaces. If the value is longer than this number, the result is not truncated. |
| * | The width is not specified in the format string but is placed as an additional integer argument before the argument to be formatted. |
| .precision | Description |
|---|---|
| .number | For integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. If the value is longer than this number, the result is not truncated. A precision of 0 means no characters are written. |
对于 e、E 和 f 说明符:要在小数点后输出的小数位数。
对于 g 和 G 说明符:要输出的最大有效位数。
对于 s: 要输出的最大字符数。默认情况下,所有字符都会被输出,直到遇到末尾的空字符。
对于 c 类型:没有任何影响。
当未指定任何精度时,默认为 1。如果指定时不带有一个显式值,则假定为 0。 |
| .* | The precision is not specified in the format string but is placed as an additional integer argument before the argument to be formatted. |
| length | Description |
|---|---|
| h | The argument is interpreted as a short int or unsigned short int (only applies to integer specifiers: i, d, o, u, x, and X). |
| l | The argument is interpreted as a long int or unsigned long int, applying to integer specifiers (i, d, o, u, x, and X) and to the specifiers c (a wide character) and s (a wide-character string). |
| L | The argument is interpreted as a long double (only applies to floating-point specifiers: e, E, f, g, and G). |
- arg -- An object representing a variable argument list. This should be initialized by the
va_start macro defined in .
Return Value
If successful, it returns the total number of characters written; otherwise, it returns a negative number.
Example
The following example demonstrates the use of the vprintf() function.
Example
#include
#include
void
WriteFrmtd
(
char
format
,
...
)
{
va_list args
;
va_start
(
args
,
format
)
;
vprintf
(
format
,
args
)
;
va_end
(
args
)
;
}
int
main
(
)
{
WriteFrmtd
(
"%d variable argument\n"
,
1
)
;
WriteFrmtd
(
"%d variable %s\n"
,
2
,
"arguments"
)
;
return
(
0
)
;
}
Let's compile and run the above program, which will produce the following result:
1 variable argument
2 variable arguments
C Standard Library -