Customizing printf

Discussion in 'C Programming' started by jacob navia, Nov 15, 2011.

  1. jacob navia

    jacob navia Guest

    Recently I was wondering how to extend lcc-win's printf to support 128
    bit integers, and searching the net I found this:

    http://www.gnu.org/s/hello/manual/libc/Customizing-Printf.html

    Apparently gcc has an interface for customizing printf that looks quite
    interesting.

    I would like to know if anybody here has experience with using this, I
    am considering implementing it in lcc-win.

    Thanks
    jacob navia, Nov 15, 2011
    #1
    1. Advertising

  2. On 11/15/2011 08:36 AM, jacob navia wrote:
    > http://www.gnu.org/s/hello/manual/libc/Customizing-Printf.html
    >
    > Apparently gcc has an interface for customizing printf that looks quite
    > interesting.
    >
    > I would like to know if anybody here has experience with using this, I
    > am considering implementing it in lcc-win.


    I've never used GCC's (actually, the GNU C Library's) extendible printf,
    but I have used Plan 9's libfmt, which has a similar idea. A Unix port
    is available at <http://swtch.com/plan9port/unix/>. It's a nice
    extension, on occasion.

    By the way, if you generally use Microsoft's C library in lcc-win, you
    might warn your users that MS printf extensions they're used to using
    are no longer there.

    --Joel
    Joel C. Salomon, Nov 15, 2011
    #2
    1. Advertising

  3. jacob navia

    Ben Pfaff Guest

    jacob navia <> writes:

    > Recently I was wondering how to extend lcc-win's printf to support 128
    > bit integers, and searching the net I found this:
    >
    > http://www.gnu.org/s/hello/manual/libc/Customizing-Printf.html
    >
    > Apparently gcc has an interface for customizing printf that looks quite
    > interesting.


    One reason that I have never used glibc's extensible printf is
    that GCC can't be configured to type-check the arguments for
    extensions, so you get warnings for correct use of extensions.
    --
    Ben Pfaff
    http://benpfaff.org
    Ben Pfaff, Nov 15, 2011
    #3
  4. jacob navia

    jacob navia Guest

    Le 15/11/11 19:10, Ben Pfaff a écrit :
    > jacob navia<> writes:
    >
    >> Recently I was wondering how to extend lcc-win's printf to support 128
    >> bit integers, and searching the net I found this:
    >>
    >> http://www.gnu.org/s/hello/manual/libc/Customizing-Printf.html
    >>
    >> Apparently gcc has an interface for customizing printf that looks quite
    >> interesting.

    >
    > One reason that I have never used glibc's extensible printf is
    > that GCC can't be configured to type-check the arguments for
    > extensions, so you get warnings for correct use of extensions.


    This will not happen in lcc-win. Check functions can be provided that
    will be loaded by the compiler at startup or can be linked into the
    compiler.
    jacob navia, Nov 15, 2011
    #4
  5. jacob navia

    Ben Pfaff Guest

    jacob navia <> writes:

    > Le 15/11/11 19:10, Ben Pfaff a écrit :
    >> jacob navia<> writes:
    >>
    >>> Recently I was wondering how to extend lcc-win's printf to support 128
    >>> bit integers, and searching the net I found this:
    >>>
    >>> http://www.gnu.org/s/hello/manual/libc/Customizing-Printf.html
    >>>
    >>> Apparently gcc has an interface for customizing printf that looks quite
    >>> interesting.

    >>
    >> One reason that I have never used glibc's extensible printf is
    >> that GCC can't be configured to type-check the arguments for
    >> extensions, so you get warnings for correct use of extensions.

    >
    > This will not happen in lcc-win. Check functions can be provided that
    > will be loaded by the compiler at startup or can be linked into the
    > compiler.


    I'm glad to hear that you are planning to avoid that problem. I
    wish that GCC would also arrange a solution.
    --
    char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
    ={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,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 1:putchar(a[i&15]);break;}}}
    Ben Pfaff, Nov 15, 2011
    #5
  6. On 11/15/2011 03:51 PM, Ben Pfaff wrote:
    > jacob navia <> writes:
    >> Le 15/11/11 19:10, Ben Pfaff a écrit :
    >>> One reason that I have never used glibc's extensible printf is
    >>> that GCC can't be configured to type-check the arguments for
    >>> extensions, so you get warnings for correct use of extensions.

    >>
    >> This will not happen in lcc-win. Check functions can be provided that
    >> will be loaded by the compiler at startup or can be linked into the
    >> compiler.

    >
    > I'm glad to hear that you are planning to avoid that problem. I
    > wish that GCC would also arrange a solution.


    See the examples given for libfmt at
    <http://swtch.com/plan9port/unix/man/fmtinstall3.html>. The example
    includes #pragmas suitable for Ken Thompson's C compilers (as
    distributed with Plan 9 or with Google Go):

    #pragma varargck type "X" Complex

    Hard to get much simpler than that...

    JST
    Joel C. Salomon, Nov 15, 2011
    #6
  7. jacob navia

    -.- Guest

    jacob navia was trying to save the world with his stuff:

    > Recently I was wondering how to extend lcc-win's printf to support ...
    > ...am considering implementing it in lcc-win.


    You self-celebrating fucko. There only exist your things to you:
    that silly lcc-win and your funny containers.
    Stop making this newsgroup your personal advertisements page.
    -.-, Nov 16, 2011
    #7
  8. In article <nzOwq.98710$>,
    -.- <> wrote:
    >jacob navia was trying to save the world with his stuff:
    >
    >> Recently I was wondering how to extend lcc-win's printf to support ...
    >> ...am considering implementing it in lcc-win.

    >
    >You self-celebrating fucko. There only exist your things to you:
    >that silly lcc-win and your funny containers.
    >Stop making this newsgroup your personal advertisements page.


    Looks like somebody didn't get enough mommy-love.

    --
    But the Bush apologists hope that you won't remember all that. And they
    also have a theory, which I've been hearing more and more - namely,
    that President Obama, though not yet in office or even elected, caused the
    2008 slump. You see, people were worried in advance about his future
    policies, and that's what caused the economy to tank. Seriously.

    (Paul Krugman - Addicted to Bush)
    Kenny McCormack, Nov 16, 2011
    #8
  9. jacob navia

    Seebs Guest

    On 2011-11-16, -.- <> wrote:
    > jacob navia was trying to save the world with his stuff:
    >> Recently I was wondering how to extend lcc-win's printf to support ...
    >> ...am considering implementing it in lcc-win.


    > There only exist your things to you:
    > that silly lcc-win and your funny containers.
    > Stop making this newsgroup your personal advertisements page.


    I have Mr. Navia plonked because of his habit of random and unjustified
    flames, so I thank you for calling that post to my attention, since it has
    some cool ideas in a field I have previously thought about. I even got
    as far as a test implementation of a printf which let you pass formatting
    functions as arguments, although I never got around to a "register new
    characters" feature.

    -s
    --
    Copyright 2011, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    I am not speaking for my employer, although they do rent some of my opinions.
    Seebs, Nov 16, 2011
    #9
  10. jacob navia

    Phil Carmody Guest

    Seebs <> writes:
    > On 2011-11-16, -.- <> wrote:
    > > jacob navia was trying to save the world with his stuff:
    > >> Recently I was wondering how to extend lcc-win's printf to support ...
    > >> ...am considering implementing it in lcc-win.

    >
    > > There only exist your things to you:
    > > that silly lcc-win and your funny containers.
    > > Stop making this newsgroup your personal advertisements page.

    >
    > I have Mr. Navia plonked because of his habit of random and unjustified
    > flames, so I thank you for calling that post to my attention, since it has
    > some cool ideas in a field I have previously thought about. I even got
    > as far as a test implementation of a printf which let you pass formatting
    > functions as arguments, although I never got around to a "register new
    > characters" feature.


    We should swap code - I implemented the "register new characters" feature...
    (OK, there was one 'extension' character, and you registered new modifiers
    to that.)

    Phil
    --
    Unix is simple. It just takes a genius to understand its simplicity
    -- Dennis Ritchie (1941-2011), Unix Co-Creator
    Phil Carmody, Nov 17, 2011
    #10
  11. jacob navia

    Joe keane Guest

    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].
    Joe keane, Nov 20, 2011
    #11
  12. jacob navia

    Ben Pfaff Guest

    (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
    Ben Pfaff, Nov 20, 2011
    #12
  13. jacob navia

    Lauri Alanko Guest

    In article <>,
    Ben Pfaff <> 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-...lization_Horror_Story:_It_Could_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
    Lauri Alanko, Nov 20, 2011
    #13
  14. jacob navia

    Ben Pfaff Guest

    Lauri Alanko <> writes:

    > In article <>,
    > Ben Pfaff <> 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-...lization_Horror_Story:_It_Could_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,0xa67f6aaa,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 1:putchar(a[i&15]);break;}}}
    Ben Pfaff, Nov 20, 2011
    #14
  15. jacob navia

    Ben Pfaff Guest

    Dag-Erling Smørgrav <> writes:

    > (Ben Pfaff) writes:
    >> Lauri Alanko <> writes:
    >> > http://search.cpan.org/dist/Locale-Maketext/lib/Locale/Maketext/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,0xa67f6aaa,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 1:putchar(a[i&15]);break;}}}
    Ben Pfaff, Nov 20, 2011
    #15
  16. (Ben Pfaff) writes:
    > Lauri Alanko <> writes:
    > > http://search.cpan.org/dist/Locale-Maketext/lib/Locale/Maketext/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 -
    Dag-Erling Smørgrav, Nov 20, 2011
    #16
  17. On Nov 20, 3:14 pm, (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].
    Nick Keighley, Nov 21, 2011
    #17
  18. jacob navia

    Joe keane Guest

    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]
    Joe keane, Nov 21, 2011
    #18
  19. jacob navia

    Ben Pfaff Guest

    (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,0xa67f6aaa,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 1:putchar(a[i&15]);break;}}}
    Ben Pfaff, Nov 21, 2011
    #19
  20. jacob navia

    Ian Collins Guest

    On 11/22/11 11:23 AM, Joe keane wrote:
    > That makes it worse!


    Makes what worse?

    Context is good.

    --
    Ian Collins
    Ian Collins, Nov 21, 2011
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vsssandeep boggavarapu via .NET 247

    Customizing the Data Grid Control

    vsssandeep boggavarapu via .NET 247, Jun 7, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    585
    Alvin Bruney [MVP - ASP.NET]
    Jun 11, 2005
  2. ben
    Replies:
    4
    Views:
    614
    Martin Ambuhl
    Jun 26, 2004
  3. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,240
  4. azza

    printf affects following printf/s

    azza, Oct 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    431
  5. guru
    Replies:
    8
    Views:
    279
Loading...

Share This Page