portability issues with ' flag in printf

B

billposer

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.
 
A

Aki Tuomi

(e-mail address removed) 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
 
V

Vladimir S. Oka

(e-mail address removed) 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>
 
J

jacob navia

(e-mail address removed) 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
 
B

billposer

On the other hand, it is very useful. I'm wondering how people check
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.
 
B

billposer

The ' (single quote/apostrophe - ASCII 0x27) flag in *printf (which
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.
 
V

Vladimir S. Oka

(e-mail address removed) opined:

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

Please don't snip attribution lines.
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!
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>
 
A

Aki Tuomi

(e-mail address removed) kirjoitti:
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
 
V

Vladimir S. Oka

(e-mail address removed) opined:
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.
 
K

Keith Thompson

jacob navia said:
(e-mail address removed) 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.
 
J

jacob navia

Keith said:
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...
 
J

jacob navia

Vladimir said:
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.
 
K

Keith Thompson

jacob navia said:
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.
 
V

Vladimir S. Oka

jacob navia opined:
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.
 
R

Robert Latest

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
 
K

Kevin Handy

jacob said:
(e-mail address removed) 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,200
Latest member
LaraHunley

Latest Threads

Top