What you need is a variadic function, together with vprintf (unless you really want to parse the format string yourself). Here is an example they I have shamelessly ripped from the GNU libc manual which seems to be along similar lines as to what you want to do.
Code:
#include <stdio.h>
#include <stdarg.h>
void
eprintf (const char *template, ...)
{
va_list ap;
extern char *program_invocation_short_name;
fprintf (stderr, "%s: ", program_invocation_short_name);
va_start (ap, count);
vfprintf (stderr, template, ap);
va_end (ap);
}
Example usage from libc manual:
Code:
eprintf ("file `%s' does not exist\n", filename);
starg.h gives you the macros required to deal with variable numbers of arguments.
The "..." in the function definition defines the function as having a variable number of arguments from that point.
va_start sets up a pointer to the first argument.
vfprintf uses the format string supplied plus the argument pointer to do the business.
va_end signifies that the argument list is finished with.
I imagine that program_invocation_shortname thing needs sterr.h.
Bookmarks