C++, vsprintf working with changing number of arguments and large format strings

StackOverflow https://stackoverflow.com/questions/23685993

  •  23-07-2023
  •  | 
  •  

Question

I need to print to a file a variety of format strings. I will have integers associated with each string, the same number of integers as format specifiers in each string. Most strings will not have the same number of format specifiers/integers.

So given a format string along the lines of blah blah blah %d something else blah %hd blah blah blah %02hd blah %d and an argument list containing four integers that need to go in this statement, will vsprintf work its way through the argument list and format statement in parallel, placing the first list item at the first specifier, second item at the second specifier, so on and so forth. Or will it go through the list placing all items at the first specifier?

In other words are the following two statements equivalent?

sprintf(buffer,"blah blah blah %d something else blah %hd blah blah blah %02hd blah %d",a1,a2,a3,a4)

vsprintf(buffer,"blah blah blah %d something else blah %hd blah blah blah %02hd blah %d", args)

in vsprintf args would be a va_list. va_list args;.

Was it helpful?

Solution

will vsprintf work its way through the argument list and format statement in parallel, placing the first list item at the first specifier, second item at the second specifier, [etc.]

Yes, that is the result it will produce.

Or will it go through the list placing all items at the first specifier?

No. vsprintf would not be a useful function if it did that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top