Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > is there a symbolic constant telling us we are using a C89 compiler?

Reply
Thread Tools

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

 
 
just80n@gmail.com
Guest
Posts: n/a
 
      02-07-2006
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)

 
Reply With Quote
 
 
 
 
Vladimir S. Oka
Guest
Posts: n/a
 
      02-07-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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.

--
BR, Vladimir

 
Reply With Quote
 
 
 
 
pemo
Guest
Posts: n/a
 
      02-07-2006

"Vladimir S. Oka" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> (E-Mail Removed) wrote:
>> 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.


Which google is very good at ... here's a start:

http://groups.google.com/group/comp....t=0&scoring=d&




 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-07-2006
"Vladimir S. Oka" <(E-Mail Removed)> writes:
> (E-Mail Removed) wrote:
>> 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


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.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <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.
 
Reply With Quote
 
Jordan Abel
Guest
Posts: n/a
 
      02-07-2006
On 2006-02-07, (E-Mail Removed) <(E-Mail Removed)> wrote:
> 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.]

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

 
Reply With Quote
 
Jordan Abel
Guest
Posts: n/a
 
      02-07-2006
On 2006-02-07, Keith Thompson <(E-Mail Removed)> wrote:
> "Vladimir S. Oka" <(E-Mail Removed)> writes:
>> (E-Mail Removed) wrote:
>>> 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

>
> 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
 
Reply With Quote
 
Robin Haigh
Guest
Posts: n/a
 
      02-08-2006

"Vladimir S. Oka" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...

> (E-Mail Removed) wrote:
>
> > 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 ?


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


--
RSH


 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      02-08-2006
Jordan Abel wrote:
> On 2006-02-07, (E-Mail Removed) <(E-Mail Removed)> wrote:
> > ...
> > 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, ...


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

--
Peter

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
concatenate a constant to constant string using macros sinbad C Programming 7 06-19-2008 05:28 PM
C89: Compile-time constant required for initialiser? Tomás Ó hÉilidhe C Programming 22 12-24-2007 12:57 AM
Telling PC To Start Using DSL Again ? Robert11 Computer Support 3 03-20-2005 07:07 PM
Any big differences between final draft before C89 and actual C89 standard? G Patel C Programming 1 02-07-2005 11:03 PM
Firefox 0.9.1 keeps telling me to update! Axl Firefox 1 07-02-2004 04:13 PM



Advertisments