Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Customizing printf

Reply
Thread Tools

Customizing printf

 
 
Joe keane
Guest
Posts: n/a
 
      11-20-2011
But overall, the variadic-function-with-format-string thing seems to
lose its appeal when it's pushed past basic printf, and even there i'm
not so sure it's your friend.

Better to use fputc, fputs, fputfoo, fputbar...

You're much more likely to get compile or link errors rather than just
screwy behavior [e.g. what if some library says printf("%z", z) means
one thing and another library says it means a different thing?].

Or look at C++, mostly just works... I'm not recommending operator
overloading, you just need logical names for things [C++ makes up
different names for things; you just don't know what they are].
 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      11-20-2011
http://www.velocityreviews.com/forums/(E-Mail Removed) (Joe keane) writes:

> But overall, the variadic-function-with-format-string thing seems to
> lose its appeal when it's pushed past basic printf, and even there i'm
> not so sure it's your friend.
>
> Better to use fputc, fputs, fputfoo, fputbar...


The place where printf-like format strings shine versus a series
of discrete function calls is when internationalization comes
into the picture. It's relatively straightforward to allow
translators to supply alternative format strings to be used at
runtime, but there is no comparable way to allow them to supply a
different series of discrete function calls.
--
"Welcome to the wonderful world of undefined behavior, where the demons
are nasal and the DeathStation users are nervous." --Daniel Fox
 
Reply With Quote
 
 
 
 
Lauri Alanko
Guest
Posts: n/a
 
      11-20-2011
In article <(E-Mail Removed)>,
Ben Pfaff <(E-Mail Removed)> wrote:
> The place where printf-like format strings shine versus a series
> of discrete function calls is when internationalization comes
> into the picture. It's relatively straightforward to allow
> translators to supply alternative format strings to be used at
> runtime, but there is no comparable way to allow them to supply a
> different series of discrete function calls.


Printf doesn't "shine" in internationalization. Having
language-specific format strings is a woefully inadequate hack that is
often used in lieu of proper multilingual support. It's better than
nothing, sure, but hardly something to brag about. Here's a
description of some of the issues that come up:

http://search.cpan.org/dist/Locale-M..._Happen_To_You

There _are_ better, systematic and linguistically sound approaches to
internationalizing a program's messages, and in fact that is what I'm
currently working on. It will take a while, though, until the
technology is in a shape that is useful to the casual C programmer.


Lauri
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      11-20-2011
Lauri Alanko <(E-Mail Removed)> writes:

> In article <(E-Mail Removed)>,
> Ben Pfaff <(E-Mail Removed)> wrote:
>> The place where printf-like format strings shine versus a series
>> of discrete function calls is when internationalization comes
>> into the picture. It's relatively straightforward to allow
>> translators to supply alternative format strings to be used at
>> runtime, but there is no comparable way to allow them to supply a
>> different series of discrete function calls.

>
> Printf doesn't "shine" in internationalization. Having
> language-specific format strings is a woefully inadequate hack that is
> often used in lieu of proper multilingual support. It's better than
> nothing, sure, but hardly something to brag about. Here's a
> description of some of the issues that come up:
>
> http://search.cpan.org/dist/Locale-M..._Happen_To_You


Whoever wrote that page did not read the GNU gettext manual,
which explains the same problems and shows the solutions to them.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1utchar(a[i&15]);break;}}}
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      11-20-2011
Dag-Erling Smørgrav <(E-Mail Removed)> writes:

> (E-Mail Removed) (Ben Pfaff) writes:
>> Lauri Alanko <(E-Mail Removed)> writes:
>> > http://search.cpan.org/dist/Locale-M...text/TPJ13.pod

>> Whoever wrote that page did not read the GNU gettext manual,
>> which explains the same problems and shows the solutions to them.

>
> I believe gettext only supports one number per string, so it cannot
> handle the "%d files in %d directories" example. You'd have to rewrite
> it as "I scanned %d directories." + "I found %d files." or something
> similar.


That's true. In practice I have rarely found that to be a
significant limitation.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1utchar(a[i&15]);break;}}}
 
Reply With Quote
 
Dag-Erling Smørgrav
Guest
Posts: n/a
 
      11-20-2011
(E-Mail Removed) (Ben Pfaff) writes:
> Lauri Alanko <(E-Mail Removed)> writes:
> > http://search.cpan.org/dist/Locale-M...text/TPJ13.pod

> Whoever wrote that page did not read the GNU gettext manual,
> which explains the same problems and shows the solutions to them.


I believe gettext only supports one number per string, so it cannot
handle the "%d files in %d directories" example. You'd have to rewrite
it as "I scanned %d directories." + "I found %d files." or something
similar.

DES
--
Dag-Erling Smørgrav - (E-Mail Removed)
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      11-21-2011
On Nov 20, 3:14*pm, (E-Mail Removed) (Joe keane) wrote:

> But overall, the variadic-function-with-format-string thing seems to
> lose its appeal when it's pushed past basic printf, and even there i'm
> not so sure it's your friend.


you obviously haven't string C++ iostreams...

> Better to use fputc, fputs, fputfoo, fputbar...


how many parameters do fputint() and fputdouble() have?

> You're much more likely to get compile or link errors rather than just
> screwy behavior [e.g. what if some library says printf("%z", z) means
> one thing and another library says it means a different thing?].
>
> Or look at C++, mostly just works...


try printing a four digit hexadecimal number. Or a double with 3
significant digits. You can do thse things but they're awkward,

> I'm not recommending operator
> overloading, you just need logical names for things [C++ makes up
> different names for things; you just don't know what they are].


 
Reply With Quote
 
Joe keane
Guest
Posts: n/a
 
      11-21-2011
That makes it worse!

At least if the format string is compile-time constant, the compiler
-could- help you out. It's not obligated to do this check, and C offers
no guarantee if you screw up the types in a variadic function, but it's
fairly easy to implement.

If the format string is read from a file, then God help you...

[core dump]
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      11-21-2011
(E-Mail Removed) (Joe keane) writes:

> That makes it worse!
>
> At least if the format string is compile-time constant, the compiler
> -could- help you out. It's not obligated to do this check, and C offers
> no guarantee if you screw up the types in a variadic function, but it's
> fairly easy to implement.
>
> If the format string is read from a file, then God help you...


The tools that allow you to do this actually verify that the
format string read from the file is compatible with the one that
was specified at compile time.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa6 7f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1utchar(a[i&15]);break;}}}
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      11-21-2011
On 11/22/11 11:23 AM, Joe keane wrote:
> That makes it worse!


Makes what worse?

Context is good.

--
Ian Collins
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
printf affects following printf/s azza C Programming 0 10-17-2010 09:43 AM
Extracting printf(...) from (void) printf(....) guru Perl Misc 8 02-03-2009 10:37 PM
(void) printf vs printf whatluo C Programming 29 09-08-2005 05:42 PM
bus error with printf line included, error without printf line? ben C Programming 4 06-26-2004 04:42 PM
Customizing LDAP attribute in Netscape 7.1 Edoardo Tirtarahardja Firefox 0 08-04-2003 02:07 PM



Advertisments