C library functions -vprintf()
C Standard Library - <stdio.h>
Description
C Library Functionsint vprintf(const char *format, va_list arg)Sends formatted output to the standard output stream stdout using a parameter list.
Declaration
Below is the declaration of the vprintf() function.
int vprintf(const char *format, va_list arg)
Parameter
- format-- This is the string containing the text to be written to the standard output stream stdout. It can contain embedded format tags, which can be replaced by the values specified in the subsequent additional arguments and formatted as required. The format tag properties are: %[flags][width][.precision][length]specifierThe detailed explanation is as follows:
| 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 selects the appropriate representation between %e and %f |
| G | Automatically selects the appropriate representation between %E and %f |
| o | signed octal |
| s | character string |
| u | Unsigned decimal integer |
| x | Unsigned hexadecimal integer |
| X | Unsigned hexadecimal integer (uppercase letters) |
| p | Pointer address |
| n | No output |
| % | Character |
| flags | Description |
|---|---|
| - | Left-aligns the output within the given field width; the default is right-alignment (see the width sub-specifier). |
| + | Forces a plus or minus sign (+ or -) to appear before the result, i.e., positive numbers will show a + sign. By default, only negative numbers are preceded by a - sign. |
| (space) | If no sign is written, a space is inserted before the value. |
| # | When used with the o, x, or X specifiers, a non-zero value is preceded by 0, 0x, or 0X, respectively. When used with e, E, and f, it forces the output to contain a decimal point, even if there are no digits after it. By default, if there are no digits after the decimal point, the decimal point is not displayed. When used with g or G, the result is the same as with e or E, but trailing zeros are not removed. |
| 0 | Places zeros (0) on the left side of the digits that fill the padding, instead of spaces (see the width sub-specifier). |
| width | Description |
|---|---|
| (number) | The minimum number of characters to be output. If the output value is shorter than this number, the result is padded with spaces. If the output value is longer than this number, the result will not be truncated. |
| * | The width is not specified in the format string, but is provided as an additional integer value argument placed 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 written value is shorter than this number, the result is padded with leading zeros. If the written value is longer than this number, the result will not be truncated. A precision of 0 means no characters are written. For e, E, and f specifiers: the number of decimal digits to be output after the decimal point. For g and G specifiers: the maximum number of significant digits to be output. For s: the maximum number of characters to be output. By default, all characters are output until the terminating null character is encountered. For the c type: has no effect. When no precision is specified, it defaults to 1. If it is specified without an explicit value, it is assumed to be 0. |
| .* | The precision is not specified in the format string, but is provided as an additional integer value argument placed 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, applicable to integer specifiers (i, d, o, u, x, and X) and the specifiers c (indicating a wide character) and s (indicating 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 the variable argument list. It should be initialized with the va_start macro defined in <stdarg>.
Return Value
If successful, it returns the total number of characters written; otherwise, it returns a negative number.
Example
The following example demonstrates the usage of the vprintf() function.
Example
#include <stdio.h>
#include <stdarg.h>
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);
}
#include <stdarg.h>
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 us compile and run the above program, which will produce the following result:
1 variable argument 2 variable argumentsother extensions