![]() |
![]() |
![]() |
![]() |
Write formatted output to a buffer
#include <stdio.h> #include <stdarg.h> int vsprintf( char* buf, const char* format, va_list arg );
libc
The vsprintf() function formats data under control of the format control string, and writes the result to buf. The format string is described under the description of the printf() function. The vsprintf() function is equivalent to the sprintf() function, with the variable argument list replaced with arg, which has been initialized by the va_start() macro.
The number of characters written, or a negative value if an output error occurred.
The following shows the use of vsprintf() in a general error message routine:
#include <stdio.h> #include <stdarg.h> #include <stdlib.h> #include <string.h> char msgbuf[80]; char *fmtmsg( char *format, ... ) { va_list arglist; va_start( arglist, format ); strcpy( msgbuf, "Error: " ); vsprintf( &msgbuf[7], format, arglist ); va_end( arglist ); return( msgbuf ); } int main( void ) { char *msg; msg = fmtmsg( "%s %d %s", "Failed", 100, "times" ); printf( "%s\n", msg ); return EXIT_SUCCESS; }
Safety: | |
---|---|
Cancellation point | No |
Interrupt handler | Read the Caveats |
Signal handler | Read the Caveats |
Thread | Yes |
It's safe to call vsprintf() in an interrupt handler or signal handler if the data isn't floating point.
fprintf(), printf(), sprintf(), va_arg(), va_end(), va_start(), vfprintf(), vprintf()
![]() |
![]() |
![]() |
![]() |