www.digitalmars.com         C & C++   DMDScript  

D - printf issue

reply "DeadCow" <deadcow-remove-this free.fr> writes:
Im wondering why printf must be a "special case". After all, whatever the
argument type is, it will be converted to string. Why dont convert it
_before_ call printf ?

What about a printf module with

- a printf functions like:

int printf( char[] format, char[] arg );
int printf( char[] format, char[] arg1, char[] arg2 );
int printf( char[] format, char[] arg1, char[] arg2, char[] arg3 );
int printf( char[] format, char[][] args );

- a set of functions like:

...
char[] dec( ... );
char[] hex( ... );
char[] oct( ... );
...

then we can use printf like this:

int a = 10;
int b = 5;
printf( "a=%, b=%\n", hex(a), hex(b) );

... less elegant but still handy for debuging isn't it ?

For more sofisticated outputs ( padding, ... ), maybe a more specific class
can be created.
Jul 25 2003
parent reply "Sean L. Palmer" <palmer.sean verizon.net> writes:
This has been discussed almost to death here in the past.

Anything you're going to want to output to the console you're going to want
to be able to also output to a text file, OutputDebugString, a string in
memory, etc.

That tells me these things:

* Output should work the same on any stream (drop printf and just have
fprintf, or drop fprintf and make a separate function to designate which
stream is currently being printed to).
* All those things mentioned above are streams or have stream wrappers.
(cout, cin, cerr, file, debug log, string buffer)

But if you always print to strings and then send to stdio, you're wasting
performance storing the chars in the string when they really could be stored
directly into the stream buffer.

Sean

"DeadCow" <deadcow-remove-this free.fr> wrote in message
news:bfrl3n$g79$1 digitaldaemon.com...
 Im wondering why printf must be a "special case". After all, whatever the
 argument type is, it will be converted to string. Why dont convert it
 _before_ call printf ?

 What about a printf module with

 - a printf functions like:

 int printf( char[] format, char[] arg );
 int printf( char[] format, char[] arg1, char[] arg2 );
 int printf( char[] format, char[] arg1, char[] arg2, char[] arg3 );
 int printf( char[] format, char[][] args );

 - a set of functions like:

 ...
 char[] dec( ... );
 char[] hex( ... );
 char[] oct( ... );
 ...

 then we can use printf like this:

 int a = 10;
 int b = 5;
 printf( "a=%, b=%\n", hex(a), hex(b) );

 ... less elegant but still handy for debuging isn't it ?

 For more sofisticated outputs ( padding, ... ), maybe a more specific

 can be created.

Jul 25 2003
parent "Walter" <walter digitalmars.com> writes:
"Sean L. Palmer" <palmer.sean verizon.net> wrote in message
news:bfrnoh$imv$1 digitaldaemon.com...
 But if you always print to strings and then send to stdio, you're wasting
 performance storing the chars in the string when they really could be

 directly into the stream buffer.

Yup. The key to fast I/O is avoiding extra copies.
Jul 30 2003