0

What's wrong with the follwing code? How can we make the function print() to work as printf?

#include <stdio.h> #include<stdarg.h> void print(char *format,...) { va_list args; va_start(args,format); printf(format,args); } int main() { print("%d %s",5,"le"); } 
2

4 Answers 4

7

If you need to pass varargs, then use vprintf instead.

Sign up to request clarification or add additional context in comments.

Comments

3

You need vprintf there. Take a look this question, it has a similiar problem: what's the difference between the printf and vprintf function families, and when should I use one over the other?

Comments

3

You probably need to look at vprintf. That function (and the related ones) allow you to pass along a variable argument list, and they handle the formatting.

Comments

1

First of all there is a va_end() call missing which is mandatory if using va_start().

And you cannot use printf() if you want to use a va_list as argument. Take a look at vprintf().

example:

void print(char *format,...) { va_list args; va_start(args,format); vprintf(format,args); va_end(args); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.