printf wrapper

C

Colin Kern

I'm trying to write a function that is a wrapper for printf. It looks
something like:

myprintf(int myarg1, int myarg2, const char* fmt, ...)
{
/* Do something with myarg1, myarg2 *.
.
.
.

printf(fmt, ?)
}

I'm not sure how to pass the variable arguments on to printf. Any
help?

Thanks,
Colin
 
R

Ross A. Finlayson

Colin said:
I'm trying to write a function that is a wrapper for printf. It looks
something like:

myprintf(int myarg1, int myarg2, const char* fmt, ...)
{
/* Do something with myarg1, myarg2 *.
.
.
.

printf(fmt, ?)
}

I'm not sure how to pass the variable arguments on to printf. Any
help?

Thanks,
Colin

Hi,

I just learned this recently myself, you use vprintf, that's what
vprintf does. Instead of being prototyped with the ellipsis like
printf:

int printf(char* fmt, ...);

it's prototyped with a variables arguments or varargs list, va_list.

int vprintf(char* fmt, va_list ap);

So, ...:

#include <stdio.h>
#include <stdarg.h>

int myprintf(int x, int y, char* fmt, ...){

int retval=0;
va_list ap;

va_start(ap, fmt); /* Initialize the va_list */

x = y; /* Use the variables, (void)x;(void)y; */

retval = vprintf(fmt, ap); /* Call vprintf */

va_end(ap); /* Cleanup the va_list */

return retval;

}

You always have to call va_start and va_end on the argument list, the
va_list.

I think that's right, I could be wrong.

I think it's a good idea to avoid using the varargs facility, because
it leads to knowledge of how the compiler pushes function arguments
onto the stack, but if you're using the printf facility it's good for
that.

A great book for C is Harbison and Steele's "C: A Reference Manual",
it's not easy to find a clear description of the varargs facility, that
otherwise good text doesn't have one, but it's my favorite C book.

Warm regards,

Ross F.
 
M

Martin Ambuhl

Colin said:
I'm trying to write a function that is a wrapper for printf. It looks
something like:

myprintf(int myarg1, int myarg2, const char* fmt, ...)
{
/* Do something with myarg1, myarg2 *.
.
.
.

printf(fmt, ?)
}

I'm not sure how to pass the variable arguments on to printf. Any
help?

Until you can buy a text on C, ...

#include <stdio.h>
#include <stdarg.h>

void myprintf(int myarg1, int myarg2, const char *fmt, ...)
{
va_list arg;
va_start(arg, fmt);
printf("%d:%d ", myarg1, myarg2);
vprintf(fmt, arg);
va_end(arg);
}

int main(void)
{
myprintf(1, 2, " A double: %g\n", 37.4);
myprintf(399, -20, " A string: %s\n", "Tol'ja!");
return 0;
}

[output]
1:2 A double: 37.4
399:-20 A string: Tol'ja!
 
I

Itay

Colin said:
I'm trying to write a function that is a wrapper for printf. It looks
something like:

myprintf(int myarg1, int myarg2, const char* fmt, ...)
{
/* Do something with myarg1, myarg2 *.
.
.
.

printf(fmt, ?)
}

I'm not sure how to pass the variable arguments on to printf. Any
help?

Thanks,
Colin

First, you'll have to also #include <stdarg.h>
then:
myprintf(int arg1, int arg2, const char* fmt, ...)
{
//do whatever you want with arg1 and arg2
...
va_list arg;
va_start(arg, fmt);
vprintf(fmt, arg); //Pay Attantion: vprintf - not printf
va_end(arg);
}
actually, what the original printf does is:

int printf (const char *format, ...)
{
va_list arg;
int done;

va_start (arg, format);
done = vprintf (format, arg);
va_end (arg);
return done;
}

So that's just like modifying it.

HTH,
Itay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,891
Members
47,423
Latest member
henerygril

Latest Threads

Top