Description
The C library function int vsprintf(char *str, const char *format, va_list arg) sends formatted output to a string using an argument list.
Declaration
Below is the declaration of the vsprintf() function.
int vsprintf(char *str, const char *format, va_list arg)
Parameters
- str -- This is a pointer to a character array where the resulting C string is stored.
- format -- This is the string that contains the text to be written to the string
str. It can contain embedded format tags that are replaced by the values specified in subsequent additional arguments and formatted as requested. The format tag syntax is %[.precision]specifier, explained in detail below:
| specifier | Output |
|---|---|
| c | Character |
| d or i | Signed decimal integer |
| e | Scientific notation using lowercase 'e' (mantissa and exponent) |
| E | Scientific notation using uppercase 'E' (mantissa and exponent) |
| f | Decimal floating-point number |
| g | Automatically selects the appropriate representation between %e or %f |
| G | Automatically selects 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 |
| % | Literal '%' character |
| flags | Description |
|---|---|
| - | Left-aligns within the given field width; right-alignment is the default (see width specifier). |
| + | Forces a sign (+ or -) to appear before the result; positive numbers will show a '+' sign. By default, only negative numbers show a '-' sign. |
| (space) | Inserts a space before the value if no sign is written. |
| # | When used with o, x, or X specifiers, prefixes non-zero values with 0, 0x, or 0X respectively. When used with e, E, and f, forces the output to include a decimal point even if no digits follow it. By default, the decimal point is omitted if no digits follow. When used with g or G, behaves like e or E but does not remove trailing zeros. |
| 0 | Pads the number on the left with zeros (0) instead of spaces (see width specifier). |
| width | Description |
|---|---|
| (number) | Minimum number of characters to output. If the value is shorter than this number, the result is padded with spaces. If longer, the result is not truncated. |
| * | The width is not specified in the format string but is provided as an additional integer argument preceding the argument to be formatted. |
| .precision | Description |
|---|---|
| .number | For integer specifiers (d, i, o, u, x, X): specifies the minimum number of digits to write. If the value is shorter, leading zeros are added. If longer, the result is not truncated. A precision of 0 means no characters are written. For e, E, and f specifiers: number of digits to output after the decimal point. For g and G specifiers: maximum number of significant digits. For s: maximum number of characters to output. By default, all characters are output until the terminating null character. For c type: has no effect. When no precision is specified, it defaults to 1. If 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 argument preceding the argument to be formatted. |
| length | Description |
|---|---|
| h | The argument is interpreted as a short int or unsigned short int (applies only to integer specifiers: i, d, o, u, x, and X). |
| l | The argument is interpreted as a long int or unsigned long int for integer specifiers (i, d, o, u, x, and X), and as a wide character for specifier c or a wide character string for specifier s. |
| L | The argument is interpreted as a long double (applies only 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_startmacro defined in<stdarg.h>.
Return Value
If successful, returns the total number of characters written; otherwise, returns a negative number.
Example
The following example demonstrates the usage of the vsprintf() function.
#include <stdio.h>
#include <stdarg.h>
char buffer;
int vspfunc(char *format, ...)
{
va_list aptr;
int ret;
va_start(aptr, format);
ret = vsprintf(buffer, format, aptr);
va_end(aptr);
return(ret);
}
int main()
{
int i = 5;
float f = 27.0;
char str = ".com";
vspfunc("%d %f %s", i, f, str);
printf("%sn", buffer);
return(0);
}
Let's compile and run the above program, which will produce the following result:
5 27.000000 .com
YouTip