View mode: basic / threaded / horizontal-split · Log in · Help
August 16, 2001
printf
For goodness' sake, don't take printf from C verbatim.

The programmer himself must match the format specifier to the data type.
The programmer himself must align the number of the format specifiers to the
number of arguments.
The programmer himself must align the positions of the format specifiers to
the positions of arguments.

Make a printf where the format specifier is adjacent to the variable.

I have a function that works this way:

   my_printf("a=%d", (int)a, " b=%d", (int)b, (char *)NULL);

i.e. it takes a format strng containing *one* argument; the arguments(s)
following are for that format specifier; multiple pairs of format/argument
can be supplied.

Perhaps in D, the end of the variable argument list to Dprintf can be known,
rather than having the programmer be diligent to add the NULL at the end
every time.  Actually, if you add Real Macros, printf can be a macro which
knows the number and data types of the arguments:

   Dprintf("a=", a, " b=", b, "\n");

--
Richard Krehbiel, Arlington, VA, USA
rich@kastle.com (work) or krehbiel3@home.com (personal)
August 16, 2001
Re: printf
I know printf is the function that everyone loves to hate <g>, but I confess
I think printf is one of the great features of C and I'm just not willing to
give up the wonderful convenience and power of it. Believe me, I've thought
about it. I know it's not typesafe, but printf is soooo useful that it's
worth giving up ideological purity for it! -Walter

"Richard Krehbiel" <rich@kastle.com> wrote in message
news:9lgkos$251p$1@digitaldaemon.com...
> For goodness' sake, don't take printf from C verbatim.
>
> The programmer himself must match the format specifier to the data type.
> The programmer himself must align the number of the format specifiers to
the
> number of arguments.
> The programmer himself must align the positions of the format specifiers
to
> the positions of arguments.
>
> Make a printf where the format specifier is adjacent to the variable.
>
> I have a function that works this way:
>
>     my_printf("a=%d", (int)a, " b=%d", (int)b, (char *)NULL);
>
> i.e. it takes a format strng containing *one* argument; the arguments(s)
> following are for that format specifier; multiple pairs of format/argument
> can be supplied.
>
> Perhaps in D, the end of the variable argument list to Dprintf can be
known,
> rather than having the programmer be diligent to add the NULL at the end
> every time.  Actually, if you add Real Macros, printf can be a macro which
> knows the number and data types of the arguments:
>
>     Dprintf("a=", a, " b=", b, "\n");
>
> --
> Richard Krehbiel, Arlington, VA, USA
> rich@kastle.com (work) or krehbiel3@home.com (personal)
>
>
>
August 16, 2001
Re: printf
Walter wrote:
> I know printf is the function that everyone loves to hate <g>, but I confess
> I think printf is one of the great features of C and I'm just not willing to
> give up the wonderful convenience and power of it. Believe me, I've thought
> about it. I know it's not typesafe, but printf is soooo useful that it's
> worth giving up ideological purity for it! -Walter


(Apologies for the crap quoting, I haven't used newsgroups in 5 or 6 
years, and I am using Mozilla instead of something sane like tin)
I do agree that format strings with varargs.  Perhaps we can come up 
with a typesafe solution...  Maybe include format strings inside the 
actual string implementation?
August 17, 2001
Re: printf
"Matt Busigin" <mbusigin@helios.spang.org.uk> wrote in message
news:3B7C5043.2050905@helios.spang.org.uk...
> Walter wrote:
> > I know printf is the function that everyone loves to hate <g>, but I
confess
> > I think printf is one of the great features of C and I'm just not
willing to
> > give up the wonderful convenience and power of it. Believe me, I've
thought
> > about it. I know it's not typesafe, but printf is soooo useful that it's
> > worth giving up ideological purity for it! -Walter
>
>
> (Apologies for the crap quoting, I haven't used newsgroups in 5 or 6
> years, and I am using Mozilla instead of something sane like tin)
> I do agree that format strings with varargs.  Perhaps we can come up
> with a typesafe solution...  Maybe include format strings inside the
> actual string implementation?

Every time I try this, it just winds up looking ugly. (Also, lots of what I
use printf's for are debugging. It is so convenient to just bang out a
printf and not worry about types, after all, it's usually just temporary
anyway.)
August 17, 2001
Re: printf
"Walter" <walter@digitalmars.com> wrote in message
news:9lh357$2n8u$1@digitaldaemon.com...
> I know printf is the function that everyone loves to hate <g>, but I
confess
> I think printf is one of the great features of C and I'm just not willing
to
> give up the wonderful convenience and power of it. Believe me, I've
thought
> about it. I know it's not typesafe, but printf is soooo useful that it's
> worth giving up ideological purity for it! -Walter

What is wrong with creating a typesafe facillity (similar to C++'s
iostreams) that would be used as following:

int i = 35;
print << "i = " << i;

rather than the much more complicated and entirely unsafe:

int i = 35;
printf("i = %d", i);

I also find the former much clearer and easier to read.

I know it is possible to introduce typesafe streams such as this (as C++ is
a working example).  If the relative complexity of C++ streams is a source
of objection by some, it might even be worthwhile considering ways of
simplifying the C++ model -- as D is starting from a (relatively) clean
slate, afterall.

- Eric Friedman
August 17, 2001
Re: printf
For starters, print << "i = " << i is ugly.

But then, count the function calls. In the statement above, you have two. In
the case of printf, you have one. Function calls (in particular through shared
libraries) tend to be expensive things.

As I documented in another thread, I state that the correct model is:
   print "i=", i

Formatting is a different topic. There are many choices, all of them can be
made type safe and using a single function call to the output function.


Christophe


erk wrote:

> "Walter" <walter@digitalmars.com> wrote in message
> news:9lh357$2n8u$1@digitaldaemon.com...
> > I know printf is the function that everyone loves to hate <g>, but I
> confess
> > I think printf is one of the great features of C and I'm just not willing
> to
> > give up the wonderful convenience and power of it. Believe me, I've
> thought
> > about it. I know it's not typesafe, but printf is soooo useful that it's
> > worth giving up ideological purity for it! -Walter
>
> What is wrong with creating a typesafe facillity (similar to C++'s
> iostreams) that would be used as following:
>
> int i = 35;
> print << "i = " << i;
>
> rather than the much more complicated and entirely unsafe:
>
> int i = 35;
> printf("i = %d", i);
>
> I also find the former much clearer and easier to read.
>
> I know it is possible to introduce typesafe streams such as this (as C++ is
> a working example).  If the relative complexity of C++ streams is a source
> of objection by some, it might even be worthwhile considering ways of
> simplifying the C++ model -- as D is starting from a (relatively) clean
> slate, afterall.
>
>  - Eric Friedman
August 17, 2001
Re: printf
>  I know it's not typesafe, but printf is soooo useful that it's
> worth giving up ideological purity for it! -Walter

I also love using printf and sprintf from a convenience standpoint, but 
it's poison for internationalization because it hardcodes in the 
parameter order.  Any localizer will point out that string substitution 
in commercial code requires that parameters be able to be reordered.

For example, 

 printf("%s %d", pMonth, nDay); // July 3

frequently becomes

 printf("%d de %s", nDay, pMonth); // 3 de Julio

during localization, but printf precludes reordering of the parameters 
in the string without recompiling.  

A localizable string formatting function ends up looking like

 printlocalized("%1 %2", param1, param2)

so the parameters can be reordered in the format string or even omitted 
from the format by the translation team without the code needing to be 
recompiled.  

Unfortunately, few C programmers realize this, so they assume that using 
printf for generating strings is reasonable.

Greg Robbins
August 17, 2001
Re: printf
The C++ iostream libraries are appalling.  It's fine if you want cout << i;
but something requiring format changes, padding changes, floating point
accuracy specification, etc... turns a one line printf statement into a
half-page cout nightmare with a method call against the stream for each
individual format change.

Peter.


"erk" <ebf@users.sourceforge.net> wrote in message
news:9li454$kbu$1@digitaldaemon.com...
> "Walter" <walter@digitalmars.com> wrote in message
> news:9lh357$2n8u$1@digitaldaemon.com...
> > I know printf is the function that everyone loves to hate <g>, but I
> confess
> > I think printf is one of the great features of C and I'm just not
willing
> to
> > give up the wonderful convenience and power of it. Believe me, I've
> thought
> > about it. I know it's not typesafe, but printf is soooo useful that it's
> > worth giving up ideological purity for it! -Walter
>
> What is wrong with creating a typesafe facillity (similar to C++'s
> iostreams) that would be used as following:
>
> int i = 35;
> print << "i = " << i;
>
> rather than the much more complicated and entirely unsafe:
>
> int i = 35;
> printf("i = %d", i);
>
> I also find the former much clearer and easier to read.
>
> I know it is possible to introduce typesafe streams such as this (as C++
is
> a working example).  If the relative complexity of C++ streams is a source
> of objection by some, it might even be worthwhile considering ways of
> simplifying the C++ model -- as D is starting from a (relatively) clean
> slate, afterall.
>
>  - Eric Friedman
>
>
August 17, 2001
Re: printf
Walter wrote:
> I know printf is the function that everyone loves to hate <g>, but I confess
> I think printf is one of the great features of C and I'm just not willing to
> give up the wonderful convenience and power of it. Believe me, I've thought
> about it. I know it's not typesafe, but printf is soooo useful that it's
> worth giving up ideological purity for it! -Walter
> ...
> 

Printf has a lot going for it.  The only thing I've been able to imagine 
that could be better would be a buffer processing version.  This would 
includ the current version as one special form, but would also have 
buffer fill (i.e., "clear the current buffer, and put this string in for 
processing")

So, in addition to:
Print.f (fmtString, itms...)
one would have the specialized forms:
Print.c (fmtString);    :: create a print buffer with contents fmtString.

Print.p (pos, itm);     :: process the format string, replacing the 
pos-th item with itm according to the specified format.
and, of course,

Print.doIt;             :: print out the current format string, as 
processed.
August 17, 2001
Re: printf
Pascals write() function was the best output mechanism I've ever seen, 
however it's difficult to implement.

For Pascal it was a hack in the compiler itself, the user could not 
even declare himself a function that behaved like write() or writeln()
« First   ‹ Prev
1 2
Top | Discussion index | About this forum | D home