call printf using va_list
void TestPrint(char* format, ...)
{
va_list argList;
va_start(argList, format);
printf(format, argList);
va_end(argList);
}
int main()
{
TestPrint("Test print %s %d\n", "string", 55);
return 0;
}
I need to get:
Test print string 55
Actually, I get garbage output. What is wrong in this code?
Use vprintf()
instead.
Instead of printf
, I recommend you try vprintf
instead, which was created for this specific purpose:
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
void errmsg( const char* format, ... )
{
va_list arglist;
printf( "Error: " );
va_start( arglist, format );
vprintf( format, arglist );
va_end( arglist );
}
int main( void )
{
errmsg( "%s %d %s", "Failed", 100, "times" );
return EXIT_SUCCESS;
}
Source