#define for C99?

Discussion in 'C Programming' started by Tom St Denis, Nov 21, 2006.

  1. Tom St Denis

    Tom St Denis Guest

    I'm adding UTF-8 support to my crypto lib and I want to avoid dying on
    pre-C99 platforms. I plan to just typedef wchar_t to unsigned long for
    them. Is there a #define for C99 compliance I could ifdef around to
    see if it's available?

    e.g.

    #ifndef __C99__
    typedef unsigned long wchar_t;
    #else
    #include <wchar.h>
    #endif

    Thanks,
    Tom
    Tom St Denis, Nov 21, 2006
    #1
    1. Advertising

  2. On Mon, 20 Nov 2006, Tom St Denis wrote:
    >
    > I'm adding UTF-8 support to my crypto lib and I want to avoid dying on
    > pre-C99 platforms. I plan to just typedef wchar_t to unsigned long for
    > them. Is there a #define for C99 compliance I could ifdef around to
    > see if it's available?
    > e.g.
    >
    > #ifndef __C99__


    You want
    #if __STDC_VERSION__ >= 199901L

    __STDC_VERSION__ was 199409L in C95, and undefined (thus macro-expanding
    to zero) in C90. I'm sure this is in a FAQ somewhere. It's certainly
    in the Standard (and drafts thereof).

    HTH,
    -Arthur
    Arthur J. O'Dwyer, Nov 21, 2006
    #2
    1. Advertising

  3. Arthur J. O'Dwyer wrote:
    > On Mon, 20 Nov 2006, Tom St Denis wrote:
    > > I'm adding UTF-8 support to my crypto lib and I want to avoid dying on
    > > pre-C99 platforms. I plan to just typedef wchar_t to unsigned long for
    > > them. Is there a #define for C99 compliance I could ifdef around to
    > > see if it's available?
    > > e.g.
    > >
    > > #ifndef __C99__

    >
    > You want
    > #if __STDC_VERSION__ >= 199901L


    ITYM <=

    > __STDC_VERSION__ was 199409L in C95, and undefined (thus macro-
    > expanding to zero) in C90.


    C90 did not require an implementation to define it, but since it is a
    reserved
    identifier, there is nothing preventing a C90 implementation from
    defining
    whatever it wants for __STDC_VERSION__.

    One alternative is to check for SIZE_MAX in <limits.h>. A conforming
    C90
    implementation cannot define that macro; a C99 implementation must.

    Since it sounds like the OP is really checking for whcar_t, the
    WCHAR_MAX
    would distinguish C94/95/99 implementations from C90 ones.

    --
    Peter
    Peter Nilsson, Nov 21, 2006
    #3
  4. Tom St Denis

    Tom St Denis Guest

    Peter Nilsson wrote:
    > One alternative is to check for SIZE_MAX in <limits.h>. A conforming
    > C90
    > implementation cannot define that macro; a C99 implementation must.
    >
    > Since it sounds like the OP is really checking for whcar_t, the
    > WCHAR_MAX
    > would distinguish C94/95/99 implementations from C90 ones.


    Thanks to both, testing for WCHAR_MAX is probably the simplest. For my
    purposes all I'm doing is encoding/decoding ASN.1 so all the string
    routines [e.g., wcstrcmp or whatever] don't matter to me. Ideally I'd
    like to use wchar_t for people with C99 platforms so they don't have to
    cast or convert to the proper type to use the C functions for wchar
    strings.

    Coolies.

    Thanks,
    Tom
    Tom St Denis, Nov 21, 2006
    #4
  5. Tom St Denis wrote:
    > I'm adding UTF-8 support to my crypto lib and I want to avoid dying on
    > pre-C99 platforms. I plan to just typedef wchar_t to unsigned long for
    > them. Is there a #define for C99 compliance I could ifdef around to
    > see if it's available?
    >
    > e.g.
    >
    > #ifndef __C99__
    > typedef unsigned long wchar_t;
    > #else
    > #include <wchar.h>
    > #endif


    But wchar_t is part of C89 and the more complete library support for
    wide characters appeared in C94. Why would testing for C99 be relevant?
    J. J. Farrell, Nov 21, 2006
    #5
  6. Tom St Denis

    Tom St Denis Guest

    Tom St Denis wrote:
    > Peter Nilsson wrote:
    > > One alternative is to check for SIZE_MAX in <limits.h>. A conforming
    > > C90
    > > implementation cannot define that macro; a C99 implementation must.
    > >
    > > Since it sounds like the OP is really checking for whcar_t, the
    > > WCHAR_MAX
    > > would distinguish C94/95/99 implementations from C90 ones.

    >
    > Thanks to both, testing for WCHAR_MAX is probably the simplest. For my
    > purposes all I'm doing is encoding/decoding ASN.1 so all the string
    > routines [e.g., wcstrcmp or whatever] don't matter to me. Ideally I'd
    > like to use wchar_t for people with C99 platforms so they don't have to
    > cast or convert to the proper type to use the C functions for wchar
    > strings.


    Just a follow up ... what I have so far ... :-(

    #if (defined(SIZE_MAX) || __STDC_VERSION__ >= 199901L ||
    defined(WCHAR_MAX) || defined(_WCHAR_T) || defined(_WCHAR_T_DEFINED))
    && !defined(LTC_NO_WCHAR)
    #include <wchar.h>
    #else
    typedef ulong32 wchar_t;
    #endif

    [excuse the wrapped lines...]

    It seems that gcc will not natively define __STDC_VERSION [or to match
    the restraint]. Also WCHAR_MAX is only defined if you explicitly
    include wchar.h ... (my guess is you need to force --std=c99 for that
    to show up...)

    _WCHAR_T is defined through some standard glibc headers and
    _WCHAR_T_DEFINED through some VC6 headers....

    Tom
    Tom St Denis, Nov 26, 2006
    #6
    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. theotyflos
    Replies:
    3
    Views:
    453
    Thomas Matthews
    Feb 19, 2004
  2. Replies:
    3
    Views:
    3,654
    Chris Torek
    Feb 20, 2006
  3. robin liu
    Replies:
    3
    Views:
    813
    Robin Liu
    Apr 21, 2006
  4. Replies:
    3
    Views:
    584
    Keith Thompson
    Mar 31, 2007
  5. Brian Takita

    #define _ and #define __

    Brian Takita, Jan 23, 2006, in forum: Ruby
    Replies:
    0
    Views:
    447
    Brian Takita
    Jan 23, 2006
Loading...

Share This Page