69,382
社区成员
发帖
与我相关
我的任务
分享
// crt_vsprintf.c
// compile with: /W3
// This program uses vsprintf to write to a buffer.
// The size of the buffer is determined by _vscprintf.
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
void test( char * format, ... )
{
va_list args;
int len;
char *buffer;
// retrieve the variable arguments
va_start( args, format );
len = _vscprintf( format, args ) // _vscprintf doesn't count
+ 1; // terminating '\0'
buffer = (char*)malloc( len * sizeof(char) );
vsprintf( buffer, format, args ); // C4996
// Note: vsprintf is deprecated; consider using vsprintf_s instead
puts( buffer );
free( buffer );
}
int main( void )
{
test( "%d %c %d", 123, '<', 456 );
test( "%s", "This is a string" );
}