The independent variables in the vsprintf () function are in the array, and the percent sign (%) must be added before the string of the array element ). This function is executed in sequence by step [step-by-step. After the first %, the first array element will be inserted; after the second %, the second array element will be inserted, and so on.
Vsprintf is a deformation of sprintf. It has only three parameters. Vsprintf is used to execute a custom function with multiple parameters, similar to the printf format. The first two parameters of vsprintf are the same as those of sprintf: A character buffer used to save the result and a formatted string. The third parameter is the indicator pointing to the formatted parameter array. In fact, this indicator points to the variable that provides the function call in the stack. Va_list, va_start, and va_end (defined in stdarg. h) help us deal with stacked metrics. The last scrnsize program in this chapter shows how to use these huge sets. Using vsprintf, sprintf can be written as follows:
Int sprintf (char * szbuffer, const char * szformat ,...) {int ireturn; va_list pargs; va_start (pargs, szformat); ireturn = vsprintf (szbuffer, szformat, pargs); va_end (pargs); Return ireturn ;}
The va_start giant set parg to point to a stack variable, which is located on the top of the stack parameter szformat.
Function Name: vsprintf
Function: Send formatted output to the string
Usage: int vsprintf (char * string, char * format, va_list PARAM );
Program Example:
# Include <stdio. h>
# Include <conio. h>
# Include <stdarg. h>
Char buffer [80];
Int vspf (char * FMT ,...)
{
Va_list argptr;
Int CNT;
Va_start (argptr, FMT );
CNT = vsprintf (buffer, FMT, argptr );
Va_end (argptr );
Return (CNT );
}
Int main (void)
{
Int inumber = 30;
Float fnumber = 90.0;
Char string [4] = "ABC ";
Vspf ("% d % F % s", inumber, fnumber, string );
Printf ("% s \ n", buffer );
Return 0;
}
Va_list AP;
Int Len;
Va_start (AP, format );
Vsprintf (_ this-> printfbuf, format, AP)
Va_end (AP );
What is the role of vsprintf? Can you elaborate on the code above? Thank you!
====================================
Set Parameters The AP adopts the format specified by format,
Write to _ this-> printfbuf
Basically, it is similar to sprinf ......
For example:
Vsprintf (buffer, FMT, argptr );
FMt = "% d % F % s"
The following parameters are output to the buffer in the format of "% d % F % s ".
Note: This blog post references others.