CKTIS / C
vsprintf — formatting into a buffer using va_list
vsprintf writes formatted text into a buffer, obtaining arguments from a va_list. It takes no buffer size.
Version: C23; repository compiler flag, production runtime not attested
Syntax
#include <stdarg.h>
#include <stdio.h>
int vsprintf(char * restrict s, const char * restrict format, va_list arg);vsprintf works like sprintf but accepts a va_list instead of ... arguments. It also writes a terminating null character into s.
The buffer must hold the complete result plus the null character. Overflow and copying between overlapping objects cause undefined behavior. Argument types must match format specifiers. Do not use untrusted text as format; choose vsnprintf to bound writes.
Initialize the list with va_start. The function does not call va_end: do that yourself. Do not read the list again afterward; prepare a copy beforehand with va_copy for an independent traversal.
The return value counts written characters excluding the terminator, or is negative on an encoding error.
Examples
#include <stdarg.h>
#include <stdio.h>
/* Only for calls whose complete output fits in dst. */
static int format_text(char *dst, const char *format, ...)
{
va_list args;
va_start(args, format);
int result = vsprintf(dst, format, args);
va_end(args);
return result;
}
int main(void)
{
char text[16];
int count = format_text(text, "score=%d", 7);
if (count < 0) {
return 1;
}
puts(text);
printf("%d\n", count);
return 0;
}
Result
score=7 7
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.