The function vsprintf () in C

The function int vsprintf (char * str, const char * format, va_list arg) in the standard C library sends the formatted output to a string string using a parameter list.

The function int vsprintf (char * str, const char * format, va_list arg) in the standard C library sends the formatted output to a string string using a parameter list.

Declare the function vsprintf () in C

Below is the declaration for vsprintf () function in C:

 int vsprintf ( char * str , const char * format , va_list arg ) 

Parameters

str - This is the array of char elements where the result string is stored.

format - This is the string containing text to be written to str. It may optionally contain formatting tags that can be embedded that are replaced by values ​​defined in subsequent additional parameters and formatted as required. Prototype tags of the format % [flags] [width] [. Precision] [length] specifier , explained as follows:

Character specifier Character d or i Decimal integer with the sign e Scientific symbol (mantissa / exponent) uses the character e E Scientific notation (mantissa / exponent) using characters E f Actual floating point number Decimal g Short use of% e or% f G Compact use of% E or% fo Octal number signed s Character string u Unsigned decimal number x Hexadecimal integers mark X Unsigned hexadecimal integers (uppercase letters) p Pointer address n Do not print anything% characters

 

flags Description - Align left into the given field width. The alignment must be the default + Forced to precede the result with a plus or minus (+ or -) even with positive numbers. By default, only negative numbers are preceded by a - (space). If no symbols are written, then a space will be inserted before the value # Used with the specifier o, x or X. The value is preceded by 0, 0x or 0X corresponding to values ​​other than 0. Using with e, E and f, it forces the recorded output to acquire a decimal pointer even if not any number followed. By default, if no digit follows, no decimal pointer is written. Use with g or G, the result is the same as e or E but the zeroes at the end do not have to be removed from the left 0 pad (left-pad) of the number with the 0s instead of the spaces

 

width Description (number) The minimum number of characters to be printed. If the value to be printed is shorter than this number, the result is padded with spaces. The value is not cut even if the result is too large. The width is not specified in the format format string, but as an additional integer value parameter preceding that parameter must be formatted

 

.precision Description.number For Integer Specifiers (d, i, o, u, x, X) - then Precision specifies the minimum number of digits to be written. If the recorded value is shorter than this number, the result is padded with 0 at the beginning. Value is not cut even if the result is longer. A precision of 0 means that no character is written to the value 0. For e, E and f specifier: this is the number of digits to be printed after the decimal point. With g and G specifier: this is the maximum number of digits to be printed. With s specifier: this is the minimum number of characters to be printed. By default, all characters are printed until the last null character is encountered. With type c: it has no effect. When no precision is specified, the default is 1. If the period is specified without a clear precision value, 0 is assumed. * Precision is not specified in the format format string, but as an additional raw value parameter precedes that parameter that must be formatted

 

length Description The parameter is interpreted as a short int or unsigned short int (applied only to integer specifiers: i, d, o, u, x and X) l Parameters are interpreted as a long int or unsigned long int for integer specifier (i, d, o, u, x and X), and as a wide char or wide char string for specifiers are c and s L The parameter is interpreted as a long double (applied only to floating point specifier: e, E, f, g and G)

arg - An object representing the variable parameter list. It should be initialized by the va_start macro defined in.

Returns the value

If successful, the total number of characters recorded will be returned, otherwise a negative number will be returned.

For example

The following program C illustrates the usage of the vsprintf () function in C:

 #include #include char buffer [ 80 ]; 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 [ 50 ] = "QTM xin chao cac ban !!!" ; vspfunc ( "%d %f %s" , i , f , str ); printf ( "%sn" , buffer ); return ( 0 ); } 

Compile and run the above C program to see the results.

According to Tutorialspoint

Previous lesson: Function vfprintf () in C

Next lesson: Function fscanf () in C

5 ★ | 1 Vote