portability issues with ' flag in printf

Discussion in 'C Programming' started by billposer@alum.mit.edu, Apr 19, 2006.

  1. Guest

    The ' (single quote/apostrophe - ASCII 0x27) flag in *printf (which
    causes delimitation of integer groups as per the locale) is not in the
    C standard, and as applied to floats is not even in SUSv2. On the other
    hand, it is very useful. I'm wondering how people check for
    availability of a version of printf that supports it. I don't find a
    macro for this in the autoconf macro library, and I'm hard put to write
    my own because all of the systems to which I have easy access use
    versions of gcc that support it so I don't know what the failure mode
    is on systems that do not. How do other people handle this? Is there an
    autoconf macro I don't know about? Or can someone who uses a compiler
    that doesn't support it tell me whether it generates a syntax error at
    compile time or a runtime error? Thanks.
     
    , Apr 19, 2006
    #1
    1. Advertising

  2. Aki Tuomi Guest

    kirjoitti:
    > The ' (single quote/apostrophe - ASCII 0x27) flag in *printf (which
    > causes delimitation of integer groups as per the locale) is not in the
    > C standard, and as applied to floats is not even in SUSv2. On the other
    > hand, it is very useful. I'm wondering how people check for
    > availability of a version of printf that supports it. I don't find a
    > macro for this in the autoconf macro library, and I'm hard put to write
    > my own because all of the systems to which I have easy access use
    > versions of gcc that support it so I don't know what the failure mode
    > is on systems that do not. How do other people handle this? Is there an
    > autoconf macro I don't know about? Or can someone who uses a compiler
    > that doesn't support it tell me whether it generates a syntax error at
    > compile time or a runtime error? Thanks.
    >


    Write your own test, see http://www.gnu.org/software/autoconf for
    manual. It really isn't as hard as you might think.

    Aki Tuomi
     
    Aki Tuomi, Apr 19, 2006
    #2
    1. Advertising

  3. opined:

    > The ' (single quote/apostrophe - ASCII 0x27) flag in *printf (which
    > causes delimitation of integer groups as per the locale) is not in
    > the C standard, and as applied to floats is not even in SUSv2.


    That'd make it off-topic here.

    > On the other hand, it is very useful. I'm wondering how people check
    > for availability of a version of printf that supports it.


    By reading the documentation of their particular implementation?

    > I don't find a macro for this in the autoconf macro library, and I'm
    > hard put to write my own because all of the systems to which I have
    > easy access use versions of gcc that support it so I don't know what
    > the failure mode is on systems that do not. How do other people
    > handle this? Is there an autoconf macro I don't know about?


    All off-topic here.

    > Or can someone who uses a compiler that doesn't support it tell me
    > whether it generates a syntax error at compile time or a runtime
    > error?


    Presumably, people using implementations not supporting this (or who
    never had use for it, as is my case), won't be familiar with the
    correct syntax to check for you and tell the results.

    Consider posting a legal `printf()` call using this. Maybe someone will
    try it out and tell you what they got.

    --
    "Are [Linux users] lemmings collectively jumping off of the cliff of
    reliable, well-engineered commercial software?"
    (By Matt Welsh)

    <http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
     
    Vladimir S. Oka, Apr 19, 2006
    #3
  4. jacob navia Guest

    a écrit :
    > The ' (single quote/apostrophe - ASCII 0x27) flag in *printf (which
    > causes delimitation of integer groups as per the locale) is not in the
    > C standard, and as applied to floats is not even in SUSv2. On the other
    > hand, it is very useful. I'm wondering how people check for
    > availability of a version of printf that supports it. I don't find a
    > macro for this in the autoconf macro library, and I'm hard put to write
    > my own because all of the systems to which I have easy access use
    > versions of gcc that support it so I don't know what the failure mode
    > is on systems that do not. How do other people handle this? Is there an
    > autoconf macro I don't know about? Or can someone who uses a compiler
    > that doesn't support it tell me whether it generates a syntax error at
    > compile time or a runtime error? Thanks.
    >



    #include <stdio.h>
    #include <string.h>
    int main(void)
    {
    int ll = 87334455;
    char buf[256];

    sprintf(buf,"%'d\n",ll);
    if (strchr(buf,'\''))
    printf("This compiler does NOT support the ' qualifier\n");
    else
    printf("This compiler supports the ' qualifier\n");

    }

    Using Microsoft CL I obtain
    This compiler does NOT support the ' qualifier
    Using lcc-win32 I obtain
    This compiler supports the ' qualifier
    Using gcc I obtain
    This compiler supports the ' qualifier


    jacob
     
    jacob navia, Apr 19, 2006
    #4
  5. Guest

    >Write your own test, see http://www.gnu.org/software/autoconf for
    >manual. It really isn't as hard as you might think.


    I've written my own tests in the past, but I've always known what sort
    of error to expect. If you don't, as in this case, the macro becomes
    nastier.
     
    , Apr 19, 2006
    #5
  6. Guest

    >> On the other hand, it is very useful. I'm wondering how people check
    > >for availability of a version of printf that supports it.


    >By reading the documentation of their particular implementation?


    I guess you don't ever try to write portable C. I can't read the
    documentation for
    every C implementation. The idea is to anticipate implementations that
    I don't know about.

    >Presumably, people using implementations not supporting this (or who
    >never had use for it, as is my case), won't be familiar with the
    >correct syntax to check for you and tell the results.


    Many people have used more than one implementation.
     
    , Apr 19, 2006
    #6
  7. Guest

    Thanks very much. That is helpful.
     
    , Apr 19, 2006
    #7
  8. Guest

    >> The ' (single quote/apostrophe - ASCII 0x27) flag in *printf (which
    >> causes delimitation of integer groups as per the locale) is not in
    > >the C standard, and as applied to floats is not even in SUSv2.


    >That'd make it off-topic here.


    I don't agree. "The group's focus is on how to write portable C code",
    which is
    exactly what my question is about. Furthermore, I don't see that any
    other
    group is appropriate. The question is not specific to a particular
    compiler, OS,
    processor or ABI.
     
    , Apr 19, 2006
    #8
  9. opined:

    By pure chance, I remember I wrote this not long ago...

    Please don't snip attribution lines.

    >>> On the other hand, it is very useful. I'm wondering how people
    >>> check for availability of a version of printf that supports it.

    >
    >>By reading the documentation of their particular implementation?

    >
    > I guess you don't ever try to write portable C. I can't read the
    > documentation for every C implementation. The idea is to anticipate
    > implementations that I don't know about.


    You know, you must be right. Now, where's that crystal ball got now!

    >>Presumably, people using implementations not supporting this (or who
    >>never had use for it, as is my case), won't be familiar with the
    >>correct syntax to check for you and tell the results.

    >
    > Many people have used more than one implementation.


    So?

    --
    "If you want to travel around the world and be invited to speak at a
    lot of different places, just write a Unix operating system."
    (By Linus Torvalds)

    <http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
     
    Vladimir S. Oka, Apr 19, 2006
    #9
  10. Aki Tuomi Guest

    kirjoitti:
    >>> The ' (single quote/apostrophe - ASCII 0x27) flag in *printf (which
    >>> causes delimitation of integer groups as per the locale) is not in
    >>> the C standard, and as applied to floats is not even in SUSv2.

    >
    >> That'd make it off-topic here.

    >
    > I don't agree. "The group's focus is on how to write portable C code",
    > which is
    > exactly what my question is about. Furthermore, I don't see that any
    > other
    > group is appropriate. The question is not specific to a particular
    > compiler, OS,
    > processor or ABI.
    >


    Don't use non-ANSI C then? =)

    Aki Tuomi
     
    Aki Tuomi, Apr 19, 2006
    #10
  11. opined:

    >>> The ' (single quote/apostrophe - ASCII 0x27) flag in *printf (which
    >>> causes delimitation of integer groups as per the locale) is not in
    >> >the C standard, and as applied to floats is not even in SUSv2.

    >
    >>That'd make it off-topic here.

    >
    > I don't agree. "The group's focus is on how to write portable C
    > code", which is exactly what my question is about.


    If you use a feature not specified by the C Standard your code cannot
    be portable, by definition.

    > Furthermore, I don't see that any other group is appropriate. The
    > question is not specific to a particular compiler, OS, processor or
    > ABI.


    You said (AFAIR) that GCC supports it. If it does, there are groups
    discussing GCC. If not, try to find one that does discuss your
    particular implementation.

    --
    If you didn't get caught, did you really do it?

    <http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
     
    Vladimir S. Oka, Apr 19, 2006
    #11
  12. jacob navia <> writes:
    > a écrit :
    >> The ' (single quote/apostrophe - ASCII 0x27) flag in *printf (which
    >> causes delimitation of integer groups as per the locale) is not in the
    >> C standard, and as applied to floats is not even in SUSv2. On the other
    >> hand, it is very useful. I'm wondering how people check for
    >> availability of a version of printf that supports it. I don't find a
    >> macro for this in the autoconf macro library, and I'm hard put to write
    >> my own because all of the systems to which I have easy access use
    >> versions of gcc that support it so I don't know what the failure mode
    >> is on systems that do not. How do other people handle this? Is there an
    >> autoconf macro I don't know about? Or can someone who uses a compiler
    >> that doesn't support it tell me whether it generates a syntax error at
    >> compile time or a runtime error? Thanks.
    >>

    >
    >
    > #include <stdio.h>
    > #include <string.h>
    > int main(void)
    > {
    > int ll = 87334455;
    > char buf[256];
    >
    > sprintf(buf,"%'d\n",ll);
    > if (strchr(buf,'\''))
    > printf("This compiler does NOT support the ' qualifier\n");
    > else
    > printf("This compiler supports the ' qualifier\n");
    >
    > }


    You should have a "return 0;".

    The message is slightly misleading; it's the runtime library that
    would support "%'d", not the compiler. "This implementation ..."
    would be clearer.

    <OT>
    On systems that support it, the effect of the "'" depends on the locale.
    </OT>

    On systems that don't support it, the effect of "%'d" is undefined
    behavior; you can't necessarily assume that the result will contain a
    "'" character.

    Probably a better approach is to write a program that produces some
    specific expected result if "%'d" *is* supported, and checks for that
    specific result. Tweak the locale if necessary to ensure that th
    result is distinctive. If you don't get that specific result
    (including a runtime failure or even a compilation failure), you can
    conclude that the implementation doesn't support the feature.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Apr 19, 2006
    #12
  13. jacob navia Guest

    Keith Thompson wrote:
    >
    > You should have a "return 0;".
    >


    C Standard page 13:

    " ... reaching the } that terminates the main function returns a value
    of 0. "

    I agree that return 0 would be clearer...
     
    jacob navia, Apr 19, 2006
    #13
  14. jacob navia Guest

    Vladimir S. Oka wrote:
    >
    > You said (AFAIR) that GCC supports it. If it does, there are groups
    > discussing GCC. If not, try to find one that does discuss your
    > particular implementation.
    >


    This answer is ridiculous: this person is trying to find out how to
    distinguish a compiler that supports this feature from one that doesn't.
    Asking in a gcc group will surely not help at all since gcc supports
    that feature.
     
    jacob navia, Apr 19, 2006
    #14
  15. jacob navia <> writes:
    > Keith Thompson wrote:
    >> You should have a "return 0;".
    >>

    >
    > C Standard page 13:
    >
    > " ... reaching the } that terminates the main function returns a value
    > of 0. "
    >
    > I agree that return 0 would be clearer...


    You should have a "return 0;" if your code might ever be used with a
    non-C99 implementation.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Apr 19, 2006
    #15
  16. jacob navia opined:

    > Vladimir S. Oka wrote:
    >>
    >> You said (AFAIR) that GCC supports it. If it does, there are groups
    >> discussing GCC. If not, try to find one that does discuss your
    >> particular implementation.
    >>

    >
    > This answer is ridiculous: this person is trying to find out how to
    > distinguish a compiler that supports this feature from one that
    > doesn't. Asking in a gcc group will surely not help at all since gcc
    > supports that feature.


    I don't quite agree.

    If I remember correctly, OP didn't provide a usage example. The one I
    saw (AFAIR /after/ posting the above reply) was provided by someone
    else. (I think he even ridiculed my suggestion to look it up in /his/
    compiler's documentation). With all that, I guess my reasoning was:

    People without access to compilers supporting it wouldn't necessarily
    know how to construct one, nor would their compiler's documentation
    tell them.

    People with the access to the compiler supporting it, regardless of
    whether they were familiar with the feature, could at least consult
    the documentation, and try to construct a test.

    Therefore, ask where the feature is supported.

    --
    It would seem that evil retreats when forcibly confronted
    -- Yarnek of Excalbia, "The Savage Curtain", stardate 5906.5

    <http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
     
    Vladimir S. Oka, Apr 20, 2006
    #16
  17. On 2006-04-19, jacob navia <> wrote:

    > This answer is ridiculous: this person is trying to find out how to
    > distinguish a compiler that supports this feature from one that doesn't.
    > Asking in a gcc group will surely not help at all since gcc supports
    > that feature.


    And asking in this group will surely not help either. In fact
    this group doesn't even know what a compiler is. The C99 Standard
    (or at least the n869 version) mentions the word "compiler"
    exactly once. In a footnote. Not explaining what it is.

    BTW, writing portable code means writing code that behaves
    identical on all conforming implementations. Detection of the
    presence or absence of a particular feature solves nothing: you
    still have to supply the feature (i.e., write the code) for the
    platforms that don't support it.

    robert
     
    Robert Latest, Apr 21, 2006
    #17
  18. Kevin Handy Guest

    jacob navia wrote:
    > a écrit :
    >
    >> The ' (single quote/apostrophe - ASCII 0x27) flag in *printf (which
    >> causes delimitation of integer groups as per the locale) is not in the
    >> C standard, and as applied to floats is not even in SUSv2. On the other
    >> hand, it is very useful. I'm wondering how people check for
    >> availability of a version of printf that supports it. I don't find a
    >> macro for this in the autoconf macro library, and I'm hard put to write
    >> my own because all of the systems to which I have easy access use
    >> versions of gcc that support it so I don't know what the failure mode
    >> is on systems that do not. How do other people handle this? Is there an
    >> autoconf macro I don't know about? Or can someone who uses a compiler
    >> that doesn't support it tell me whether it generates a syntax error at
    >> compile time or a runtime error? Thanks.
    >>

    >
    >
    > #include <stdio.h>
    > #include <string.h>
    > int main(void)
    > {
    > int ll = 87334455;
    > char buf[256];
    >
    > sprintf(buf,"%'d\n",ll);
    > if (strchr(buf,'\''))
    > printf("This compiler does NOT support the ' qualifier\n");
    > else
    > printf("This compiler supports the ' qualifier\n");
    >
    > }
    >
    > Using Microsoft CL I obtain
    > This compiler does NOT support the ' qualifier
    > Using lcc-win32 I obtain
    > This compiler supports the ' qualifier
    > Using gcc I obtain
    > This compiler supports the ' qualifier


    Shouldn't you also have a test to specify if it
    actually inserts the seperater characters?
    Maybe by checking the length of the result.

    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
     
    Kevin Handy, Apr 26, 2006
    #18
    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. MJL

    portability issues

    MJL, Aug 1, 2004, in forum: C++
    Replies:
    11
    Views:
    822
  2. nimdez
    Replies:
    5
    Views:
    784
    Roger Willcocks
    Aug 10, 2003
  3. Erik Leunissen
    Replies:
    9
    Views:
    334
    Walter Roberson
    Feb 25, 2005
  4. OzBob

    Portability / compatibility issues

    OzBob, Jan 15, 2006, in forum: C Programming
    Replies:
    23
    Views:
    847
    Keith Thompson
    Jan 16, 2006
  5. dank
    Replies:
    5
    Views:
    1,484
    Simon Biber
    Jun 30, 2006
Loading...

Share This Page