Is it ok to define __STDC_VERSION__?

Discussion in 'C Programming' started by =?ISO-8859-1?Q?Bj=F8rn_Augestad?=, Jul 11, 2003.

  1. Hi,

    I currently use a compiler which emits a warning on the following statement:
    #if defined(__STDC_VERSION__) || __STDC_VERSION__ < 19990601L
    ....
    #endif

    The compiler complains that __STDC_VERSION__ isn't defined. It obviously
    doesn't short circuit the expression, but evaluates both sides of the
    ||. I have, as a workaround, changed the statements into this:

    #if !defined(__STDC_VERSION__)
    #define __STDC_VERSION__ 0
    #endif

    #if __STDC_VERSION__ < 19990601L
    ....
    #endif

    Apart from any typos, is it legal to define __STDC_VERSION__ in user code?

    --
    boa

    libclc home: http://libclc.sourceforge.net
     
    =?ISO-8859-1?Q?Bj=F8rn_Augestad?=, Jul 11, 2003
    #1
    1. Advertising

  2. =?ISO-8859-1?Q?Bj=F8rn_Augestad?=

    Eric Sosman Guest

    Bjørn Augestad wrote:
    >
    > Hi,
    >
    > I currently use a compiler which emits a warning on the following statement:
    > #if defined(__STDC_VERSION__) || __STDC_VERSION__ < 19990601L
    > ...
    > #endif
    >
    > The compiler complains that __STDC_VERSION__ isn't defined. It obviously
    > doesn't short circuit the expression, but evaluates both sides of the
    > ||. [...]


    As it should, if __STDC_VERSION__ isn't defined. Are
    you sure you're not missing a `!' at the beginning of the
    test expression?

    #if !defined(__STDC_VERSION__) || ...
    ^

    --
     
    Eric Sosman, Jul 11, 2003
    #2
    1. Advertising

  3. On Fri, 11 Jul 2003, [ISO-8859-1] Bjørn Augestad wrote:
    >
    > I currently use a compiler which emits a warning on the following statement:
    > #if defined(__STDC_VERSION__) || __STDC_VERSION__ < 19990601L
    > ...
    > #endif
    >
    > The compiler complains that __STDC_VERSION__ isn't defined. It obviously
    > doesn't short circuit the expression, but evaluates both sides of the
    > ||. I have, as a workaround, changed the statements into this:
    >
    > #if !defined(__STDC_VERSION__)
    > #define __STDC_VERSION__ 0
    > #endif
    >
    > #if __STDC_VERSION__ < 19990601L
    > ...
    > #endif
    >
    > Apart from any typos, is it legal to define __STDC_VERSION__ in user code?


    Hallvard has kindly tracked down the bit of the standard that makes this
    invalid. However, an equivalent but legal workaround would be

    #ifndef __STDC_VERSION__
    #define C_VERSION_NUMBER 0
    #else
    #define C_VERSION_NUMBER __STDC_VERSION__
    #endif

    #if C_VERSION_NUMBER < 19990601L
    ....
    #endif

    or anything along those lines.

    Note that your compiler may be complaining about __STDC_VERSION__'s not
    being defined, but if it actually refuses to compile your program because
    of this, it's not conforming to the Standard in more than one way.
    I myself also subscribe to the idea of getting rid of even the most
    harmless warnings, though.

    -Arthur
     
    Arthur J. O'Dwyer, Jul 11, 2003
    #3
  4. Eric Sosman wrote:

    > Bjørn Augestad wrote:
    >
    >>Hi,
    >>
    >>I currently use a compiler which emits a warning on the following statement:
    >>#if defined(__STDC_VERSION__) || __STDC_VERSION__ < 19990601L
    >>...
    >>#endif
    >>
    >>The compiler complains that __STDC_VERSION__ isn't defined. It obviously
    >>doesn't short circuit the expression, but evaluates both sides of the
    >>||. [...]

    >
    >
    > As it should, if __STDC_VERSION__ isn't defined. Are
    > you sure you're not missing a `!' at the beginning of the
    > test expression?
    >
    > #if !defined(__STDC_VERSION__) || ...
    > ^
    >


    My apologies to all that has answered the original post, which had a
    very sloppy and incorrect illustration of the problem. For some stupid
    reason I decided to write the code instead of copying it from the source
    file, and I even mixed in elements from _POSIX_C_SOURCE. Duh.

    Here is the exact code causing problems. It's from the libclc file
    clc_settings.h and causes problems on the st20cc cross compiler v. 1.9.6
    (windows version) for the os20 embedded os:

    /* Handle differences between C89 and C99 */
    #if !defined __STDC_VERSION__ || __STDC_VERSION__ < 199901L
    # define CLC_RESTRICT
    #else
    # define CLC_RESTRICT restrict
    #endif


    Thanks again to all. I'll experiment more with the compiler on monday
    and try to come up with a portable and working solution.

    --
    boa

    libclc home: http://libclc.sourceforge.net
     
    =?ISO-8859-1?Q?Bj=F8rn_Augestad?=, Jul 11, 2003
    #4
  5. =?ISO-8859-1?Q?Bj=F8rn_Augestad?=

    Eric Sosman Guest

    Bjørn Augestad wrote:
    >
    > Here is the exact code causing problems. It's from the libclc file
    > clc_settings.h and causes problems on the st20cc cross compiler v. 1.9.6
    > (windows version) for the os20 embedded os:
    >
    > /* Handle differences between C89 and C99 */
    > #if !defined __STDC_VERSION__ || __STDC_VERSION__ < 199901L
    > # define CLC_RESTRICT
    > #else
    > # define CLC_RESTRICT restrict
    > #endif
    >
    > Thanks again to all. I'll experiment more with the compiler on monday
    > and try to come up with a portable and working solution.


    Actually, the `defined' piece is unnecessary:

    #if __STDC_VERSION__ < 199901L

    is a portable solution, because any unrecognized identifiers
    in an #if or #elif directive are replaced with zeroes.

    Another formulation found favor with pre-Standard compilers,
    some of whose preprocessors simply deleted unrecognized names
    in such expressions:

    #if __STDC_VERSION__ + 0 < 199901L

    the idea being that you'd wind up with `value + 0' or with
    `0 + 0' (the Standard answers) or with unadorned `+ 0' (for
    some pre-Standard compilers). You might want to see if this
    old dodge might calm your compiler's nervousness.

    Finally, there's Arthur O'Dwyer's suggestion, using the
    defined-ness of __STDC_VERSION__ to govern the definition of
    an "intermediate" symbol, and then testing that symbol. For
    the case at hand, you could eliminate the intermediate with
    some extra coding:

    #ifdef __STDC_VERSION__
    #if __STDC_VERSION__ >= 199901L
    #define CLC_RESTRICT restrict
    #endif
    #endif
    #ifndef CLC_RESTRICT
    #define CLC_RESTRICT /* nil */
    #endif

    This, I think, stands the best chance of quieting the
    whines -- but nothing's guaranteed; the compiler is within
    its rights to complain about the vapidity of American beer,
    if it so chooses. It must, though, accept and translate
    your code (barring other problems, of course).

    ----


    --
     
    Eric Sosman, Jul 11, 2003
    #5
  6. =?ISO-8859-1?Q?Bj=F8rn_Augestad?=

    Ed Morton Guest

    > Hi,
    >
    > I currently use a compiler which emits a warning on the following statement:
    > #if defined(__STDC_VERSION__) || __STDC_VERSION__ < 19990601L

    ^^
    Shouldn't that be an "&&"?

    Ed.
     
    Ed Morton, Jul 11, 2003
    #6
  7. On Fri, 11 Jul 2003 19:14:57 GMT, in comp.lang.c , Bjørn Augestad
    <> wrote:

    >Hi,
    >
    >I currently use a compiler which emits a warning on the following statement:
    >#if defined(__STDC_VERSION__) || __STDC_VERSION__ < 19990601L
    >...
    >#endif
    >
    >The compiler complains that __STDC_VERSION__ isn't defined.


    Is it a pre-ANSI compiler, and you're trying to use a header that
    doesn't belong to it? Did you a header from some another
    implementation?


    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
     
    Mark McIntyre, Jul 11, 2003
    #7
  8. Mark McIntyre wrote:

    > On Fri, 11 Jul 2003 19:14:57 GMT, in comp.lang.c , Bjørn Augestad
    > <> wrote:
    >
    >
    >>Hi,
    >>
    >>I currently use a compiler which emits a warning on the following statement:
    >>#if defined(__STDC_VERSION__) || __STDC_VERSION__ < 19990601L
    >>...
    >>#endif
    >>
    >>The compiler complains that __STDC_VERSION__ isn't defined.

    >
    >
    > Is it a pre-ANSI compiler,


    Pre C99, but post C89 AFAIK. ;-)
    The st20cc compiler is quite nice to work with, apart for a few quirks.

    and you're trying to use a header that
    > doesn't belong to it? Did you a header from some another
    > implementation?


    Pretty sure I don't, I'll check it though. I just get a warning about
    __STDC_VERSION__ beeing undefined when I compile the libclc source code,
    a warning I'd like to get rid of. The rest of libclc worked perfectly
    from day one, BTW. :)


    --
    boa

    libclc home: http://libclc.sourceforge.net
     
    =?ISO-8859-1?Q?Bj=F8rn_Augestad?=, Jul 12, 2003
    #8
  9. Eric Sosman wrote:

    [snip]

    >
    >
    > Actually, the `defined' piece is unnecessary:
    >
    > #if __STDC_VERSION__ < 199901L
    >
    > is a portable solution, because any unrecognized identifiers
    > in an #if or #elif directive are replaced with zeroes.


    That's what I thought too, but according to the official libclc archives
    (aka google, http://tinyurl.com/gpss), the "#if !defined" construct has
    been there since day one, code written by Dan Pop. I assume he had a
    reason to do it like that, instead of just writing
    #if __STDC_VERSION__ < 199901L ?


    [...]

    > Finally, there's Arthur O'Dwyer's suggestion, using the
    > defined-ness of __STDC_VERSION__ to govern the definition of
    > an "intermediate" symbol, and then testing that symbol. For
    > the case at hand, you could eliminate the intermediate with
    > some extra coding:
    >
    > #ifdef __STDC_VERSION__
    > #if __STDC_VERSION__ >= 199901L
    > #define CLC_RESTRICT restrict
    > #endif
    > #endif
    > #ifndef CLC_RESTRICT
    > #define CLC_RESTRICT /* nil */
    > #endif
    >
    > This, I think, stands the best chance of quieting the
    > whines -- but nothing's guaranteed; the compiler is within
    > its rights to complain about the vapidity of American beer,
    > if it so chooses. It must, though, accept and translate
    > your code (barring other problems, of course).


    Looks fool proof to me and is most likely the way to go. :)


    --
    boa

    libclc home: http://libclc.sourceforge.net
     
    =?ISO-8859-1?Q?Bj=F8rn_Augestad?=, Jul 12, 2003
    #9
  10. Emmanuel Delahaye, Jul 12, 2003
    #10
  11. =?ISO-8859-1?Q?Bj=F8rn_Augestad?=

    Dan Pop Guest

    In <> Eric Sosman <> writes:

    >Bj=F8rn Augestad wrote:
    >> =

    >
    >> Here is the exact code causing problems. It's from the libclc file
    >> clc_settings.h and causes problems on the st20cc cross compiler v. 1.9.=

    >6
    >> (windows version) for the os20 embedded os:
    >> =

    >
    >> /* Handle differences between C89 and C99 */
    >> #if !defined __STDC_VERSION__ || __STDC_VERSION__ < 199901L
    >> # define CLC_RESTRICT
    >> #else
    >> # define CLC_RESTRICT restrict
    >> #endif
    >> =

    >
    >> Thanks again to all. I'll experiment more with the compiler on monday
    >> and try to come up with a portable and working solution.

    >
    > Actually, the `defined' piece is unnecessary:
    >
    > #if __STDC_VERSION__ < 199901L
    >
    >is a portable solution, because any unrecognized identifiers
    >in an #if or #elif directive are replaced with zeroes.


    Nope, it ain't, because __STDC_VERSION__ may be any kind of preprocessor
    magic which isn't a macro. So, before attempting to use it as a macro,
    you *have* to check that a macro with this name is defined.

    Your argument would have worked for STDC_VERSION, but thingies starting
    with __ are special.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Jul 14, 2003
    #11
  12. Dan Pop wrote:
    [snip]

    >
    > You can try rewriting the code like this, but there is no guarantee that
    > the broken preprocessor won't barf again:
    >
    > #if !defined __STDC_VERSION__
    > # define CLC_RESTRICT
    > #elif __STDC_VERSION__ < 199901L
    > # define CLC_RESTRICT
    > #else
    > # define CLC_RESTRICT restrict
    > #endif
    >
    > If it works, throw in a comment explaining the goofy code.


    It didn't work. Now the compiler whines about the #elif using an
    undefined macro. I guess I can live with that one warning, as the rest
    of libclc works like a charm. :)

    Thanks.

    --
    boa

    libclc home: http://libclc.sourceforge.net
     
    =?ISO-8859-1?Q?Bj=F8rn_Augestad?=, Jul 18, 2003
    #12
    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. Tim Clacy
    Replies:
    9
    Views:
    1,220
    Ron Natalie
    Oct 27, 2003
  2. theotyflos
    Replies:
    3
    Views:
    474
    Thomas Matthews
    Feb 19, 2004
  3. robin liu
    Replies:
    3
    Views:
    825
    Robin Liu
    Apr 21, 2006
  4. Brian Takita

    #define _ and #define __

    Brian Takita, Jan 23, 2006, in forum: Ruby
    Replies:
    0
    Views:
    468
    Brian Takita
    Jan 23, 2006
  5. Francois Grieu
    Replies:
    0
    Views:
    303
    Francois Grieu
    Sep 12, 2012
Loading...

Share This Page