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

Discussion in 'C Programming' started by just80n@gmail.com, Feb 7, 2006.

  1. Guest

    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)
     
    , Feb 7, 2006
    #1
    1. Advertising

  2. 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
     
    Vladimir S. Oka, Feb 7, 2006
    #2
    1. Advertising

  3. pemo Guest

    "Vladimir S. Oka" <> wrote in message
    news:...
    > 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.lang.c/search?q=199901L&start=0&scoring=d&
     
    pemo, Feb 7, 2006
    #3
  4. Re: is there a symbolic constant telling us we are using a C89compiler?

    "Vladimir S. Oka" <> writes:
    > 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) <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, Feb 7, 2006
    #4
  5. Jordan Abel Guest

    On 2006-02-07, <> 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)
     
    Jordan Abel, Feb 7, 2006
    #5
  6. Jordan Abel Guest

    On 2006-02-07, Keith Thompson <> wrote:
    > "Vladimir S. Oka" <> writes:
    >> 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
     
    Jordan Abel, Feb 7, 2006
    #6
  7. Robin Haigh Guest

    "Vladimir S. Oka" <> wrote in message
    news:...

    > 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
     
    Robin Haigh, Feb 8, 2006
    #7
  8. Jordan Abel wrote:
    > On 2006-02-07, <> 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
     
    Peter Nilsson, Feb 8, 2006
    #8
    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. Mitch
    Replies:
    6
    Views:
    876
    rySource
    Mar 18, 2006
  2. No Spam
    Replies:
    7
    Views:
    571
    Micah Cowan
    Jan 4, 2005
  3. G Patel
    Replies:
    1
    Views:
    554
  4. Jason Curl

    Does a C89 compliant compiler exist?

    Jason Curl, Mar 15, 2005, in forum: C Programming
    Replies:
    9
    Views:
    439
    Walter Roberson
    Mar 16, 2005
  5. Tomás Ó hÉilidhe

    C89: Compile-time constant required for initialiser?

    Tomás Ó hÉilidhe, Dec 9, 2007, in forum: C Programming
    Replies:
    22
    Views:
    1,347
    David Thompson
    Dec 24, 2007
Loading...

Share This Page