is there a symbolic constant telling us we are using a C89 compiler?

J

just80n

Hi all!

In C89 standard, the snprintf function is NOT included in <stdio.h>...

Thus, using snprintf in your code and compiling with gcc -ansi leads to
the following warning:

warning: implicit declaration of function `snprintf'"

In order to nicely get rid of this bad stuff, I intended to do
something like:

#if WE_ARE_COMPILING_USING_ANSI_C89_RULES
int snprintf(char *str, size_t size, const char *format, ...);
#endif

Which is defining the prototype of the function in case stdio.h won't
do it.

Question is : is there a symbolic constant
WE_ARE_COMPILING_USING_ANSI_C89_RULES defined by gcc, or do I have to
make it up myself, and how ?


thanx
(please, I m quite a newbie, so maybe I got totally wrong)
 
V

Vladimir S. Oka

Hi all!

In C89 standard, the snprintf function is NOT included in <stdio.h>...

Thus, using snprintf in your code and compiling with gcc -ansi leads to
the following warning:

warning: implicit declaration of function `snprintf'"

In order to nicely get rid of this bad stuff, I intended to do
something like:

#if WE_ARE_COMPILING_USING_ANSI_C89_RULES
int snprintf(char *str, size_t size, const char *format, ...);
#endif

Which is defining the prototype of the function in case stdio.h won't
do it.

Question is : is there a symbolic constant
WE_ARE_COMPILING_USING_ANSI_C89_RULES defined by gcc, or do I have to
make it up myself, and how ?

The Standard specifies that a macro by the name:

__STDC_VERSION__ /* two `_`s front, and back) */

should be defined an for C90 it should be:

199901L

There was a discussion about this recently in c.l.c so look it up as
well.

PS
There should also be __STDC__ defined as 1 if implementation is
conforming. Look in section 6.10.8 of the Standard.
 
K

Keith Thompson

Vladimir S. Oka said:
The Standard specifies that a macro by the name:

__STDC_VERSION__ /* two `_`s front, and back) */

should be defined an for C90 it should be:

199901L

But be careful. A conforming C90 implementation isn't allowed to
provide a function called snprintf() in <stdio.h>, but an
implementation might provide it as an extension (e.g., if it conforms
only partially to C99).

What you really want to do (I think) is use the implementation's
snprintf() if it exists, perhaps only in a non-conforming mode.
There's no standard macro that will tell you that. An
auto-configuration system that checks directly whether snprintf is
supported might be worthwhile.
 
J

Jordan Abel

In order to nicely get rid of this bad stuff, I intended to do
something like:

#if WE_ARE_COMPILING_USING_ANSI_C89_RULES
int snprintf(char *str, size_t size, const char *format, ...);
#endif

What if your library really doesn't provide snprintf? This won't make
the link error go away.
Which is defining the prototype of the function in case stdio.h won't
do it.
Question is : is there a symbolic constant
WE_ARE_COMPILING_USING_ANSI_C89_RULES defined by gcc, or do I have to
make it up myself, and how ?

__STDC_VERSION__ < 199901L, but see above. [However, if you intend to
provide your own version of snprintf if it's not present, then you
should be fine.]
 
J

Jordan Abel

But be careful. A conforming C90 implementation isn't allowed to
provide a function called snprintf() in <stdio.h>, but an
implementation might provide it as an extension (e.g., if it conforms
only partially to C99).

What you really want to do (I think) is use the implementation's
snprintf() if it exists, perhaps only in a non-conforming mode.
There's no standard macro that will tell you that. An
auto-configuration system that checks directly whether snprintf is
supported might be worthwhile.

<offtopic>
Or there are tricks you can play with link order rules on some systems,
as I explained in an earlier thread about powf (another function that
was newly introduced with the c99 standard). See also your
implementation docs
 
R

Robin Haigh

In gcc, the -ansi option tells it not to define any names that pollute your
name space, such as snprintf. Since this is what you don't want, one option
is not to use the -ansi option. Then the prototype in the header will be
visible (assuming it's there, which is likely).

Alternatively, if you keep -ansi, it defines the macro __STRICT_ANSI__, so
you can #ifdef for that. It's non-standard, but it's not a sin to test
whether it's defined. (Commonly this macro is what is used in the header to
#ifdef out the snprintf prototype)

The Standard specifies that a macro by the name:

__STDC_VERSION__ /* two `_`s front, and back) */

should be defined an for C90 it should be:

199901L

This value is for C99 surely. The macro wasn't in C90, it was added in 1994
 
P

Peter Nilsson

Jordan said:
__STDC_VERSION__ < 199901L, ...

That's potentially unreliable since a C90 implementation can do...

#define __STDC_VERSION__ 19900101L

One alternative to using __STDC_VERSION__ is to check whether
SIZE_MAX is defined in <limits.h>.
 

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,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top