473,383 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

printf wrapper

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 *.
Nov 14 '05 #1
3 27251
Colin Kern wrote:
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.

Nov 14 '05 #2
Colin Kern wrote:
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!
Nov 14 '05 #3
Colin Kern wrote:
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
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Robert Ferrell | last post by:
I'm trying to "extend" Python by writing a bit of C code. I followed the example in the Python documentation, and almost everything works fine. I can build a DLL, fire up an interactive Python...
12
by: Egil M?ller | last post by:
Is there any way to create transparent wrapper objects in Python? I thought implementing __getattribute__ on either the wrapper class or its metaclass would do the trick, but it does not work for...
6
by: hswerdfe | last post by:
is there a standard function that formats data the same way that printf and sprintf do. only the return value is a char*/string of the formated values. I was wanting to do something like this...
2
by: rejeesh | last post by:
Hi, I need a 'wrapper' function for printf. So that anywhere in my program if i call the wrapper function it does the same thing as the regular printf function, but puts a custom message at the...
29
by: whatluo | last post by:
Hi, c.l.cs I noticed that someone like add (void) before the printf call, like: (void) printf("Timeout\n"); while the others does't. So can someone tell me whether there any gains in adding...
27
by: jacob navia | last post by:
Has anyone here any information about how arrays can be formatted with printf? I mean something besides the usual formatting of each element in a loop. I remember that Trio printf had some...
1
by: Scott McFadden | last post by:
Is there much overhead leaving in printf calls for a release build of my NT service (VC8)? This app runs as a NT service or console app so it's nice to have the console ouput when troublshooting...
20
by: Sanchit | last post by:
I want to know how does printf (stdio library function) works? Does this depand on complier (I am using gcc on Linix) Does it uses some buffer in which it stores all what needed to be printed...
43
by: Jrdman | last post by:
someone has an idea on how the printf function is programmed ?
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.