Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   #define for C99? (http://www.velocityreviews.com/forums/t445229-define-for-c99.html)

Tom St Denis 11-21-2006 12:33 AM

#define for C99?
 
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


Arthur J. O'Dwyer 11-21-2006 01:34 AM

Re: #define for C99?
 

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

Peter Nilsson 11-21-2006 02:07 AM

Re: #define for C99?
 
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


Tom St Denis 11-21-2006 02:17 AM

Re: #define for C99?
 
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


J. J. Farrell 11-21-2006 04:22 AM

Re: #define for C99?
 

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?


Tom St Denis 11-26-2006 12:58 PM

Re: #define for C99?
 

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



All times are GMT. The time now is 11:59 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57