By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,471 Members | 722 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,471 IT Pros & Developers. It's quick & easy.

portability issues with ' flag in printf

P: n/a
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 '06 #1
Share this Question
Share on Google+
17 Replies


P: n/a
bi*******@alum.mit.edu 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
Apr 19 '06 #2

P: n/a
bi*******@alum.mit.edu 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>

Apr 19 '06 #3

P: n/a
bi*******@alum.mit.edu 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
Apr 19 '06 #4

P: n/a
>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 '06 #5

P: n/a
>> 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 '06 #6

P: n/a
Thanks very much. That is helpful.

Apr 19 '06 #7

P: n/a
>> 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 '06 #8

P: n/a
bi*******@alum.mit.edu 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>

Apr 19 '06 #9

P: n/a
bi*******@alum.mit.edu 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
Apr 19 '06 #10

P: n/a
bi*******@alum.mit.edu 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>

Apr 19 '06 #11

P: n/a
jacob navia <ja***@jacob.remcomp.fr> writes:
bi*******@alum.mit.edu 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) ks***@mib.org <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.
Apr 19 '06 #12

P: n/a
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...
Apr 19 '06 #13

P: n/a
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.

Apr 19 '06 #14

P: n/a
jacob navia <ja***@jacob.remcomp.fr> 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) ks***@mib.org <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.
Apr 19 '06 #15

P: n/a
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>

Apr 20 '06 #16

P: n/a
On 2006-04-19, jacob navia <ja***@jacob.remcomp.fr> 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
Apr 21 '06 #17

P: n/a
jacob navia wrote:
bi*******@alum.mit.edu 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 =----
Apr 26 '06 #18

This discussion thread is closed

Replies have been disabled for this discussion.