Pregunta

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;
}

http://ideone.com/EsHsRO

Actually, it produces http://codepad.org/k7ld231E.
Why the foo is wrong?

¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top