You need to use vprintf
instead: int vprintf( const char* format, va_list vlist );
vprintf(format, args);
printf()
and friends are for normal use. vprintf()
and friends are for when you want to write your own printf()
-like functions.
Вопрос
Why the code below doesn't produce any output? I expected it to be 42 s
. How to fix it?
#include <cstdio>
#include <cstdlib>
#include <cstdarg>
void foo(const char* format, ...)
{
va_list args;
va_start(args, format);
printf(format, args);
va_end(args);
}
int main()
{
foo("%d %s\n", 42, "s");
return 0;
}
Actually, it produces http://codepad.org/k7ld231E.
Why the foo
is wrong?
Решение
You need to use vprintf
instead: int vprintf( const char* format, va_list vlist );
vprintf(format, args);
printf()
and friends are for normal use. vprintf()
and friends are for when you want to write your own printf()
-like functions.