Groovy hepcat Robert Gamble was jivin' on Thu, 08 Sep 2005 17:47:57
-0400 in comp.lang.c.
Re: skipping parameters with printf's a cool scene! Dig it!
There is no way to do this in C99 either. In fact, I don't even think
it would be possible to design a version that did do this with the current
stdarg facility since it would need to know the type of the value being
skipped to access arguments after it.
Sure, it could be easily done. You just have to allow a display
suppression flag in the conversion specifier; much like scanf()'s
assignment suppression flag. Then the format string would simply have
a conversion specifier with this flag coresponding to the argument to
be ignored. Simple!
Simple example:
#include <stdio.h>
#include <stdarg.h>
void prints(const char *fmt, ...)
{
va_list arg;
const char *p;
FILE *fp = stdout;
va_start(arg, fmt);
for(p = fmt; *p; p++)
{
if('%' == *p)
{
/* We have a conversion specifier. */
int ignore = 0;
const char *sarg;
int iarg;
p++;
if('!' == *p)
{
/* Flag to ignore coresponding argument. */
ignore = 1;
p++;
}
switch(*p)
{
case 's': /* string */
sarg = va_arg(arg, const char *);
if(!ignore)
{
ignore = 0;
fputs(sarg, fp);
}
break;
case 'd': /* int */
iarg = va_arg(arg, int);
if(!ignore)
{
ignore = 0;
fprintf(fp, "%d", iarg);
}
break;
}
}
else
{
fputc(*p, fp);
}
}
va_end(arg);
}
int main(void)
{
prints("%s %!s %s\n", "foo", "bar", "baz");
prints("%d %d %!d %d\n", 1, 2, 3, 4);
return 0;
}
Here the simplified printf-like function, prints(), understands two
different types of conversion specifiers; one for a string and one for
an int. If the ! flag is used, the coresponding argument will be
ignored.
This program prints the following output:
foo baz
1 2 4
--
Dig the even newer still, yet more improved, sig!
http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?