Re: Defining int64
From: Eric Sosman (Eric.Sosman_at_sun.com)
Date: 11/13/03
- Next message: uydo\(kiboga\): "how tp pass a stream into a structure"
- Previous message: Fao, Sean: "Re: writing tsr in c"
- In reply to: ESOJAY: "Re: Defining int64"
- Next in thread: Dan Pop: "Re: Defining int64"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 13 Nov 2003 12:55:28 -0500
ESOJAY wrote:
>
> It allows long long. I'm just not sure if long long is always going to be 64
> bits on any machine I port the code to.
The situation is a little bit screwy ...
The C99 Standard requires `long long' to be at least 64
bits wide, but permits it to be wider. If you need 64 bits
or more but don't want to waste space if `long long' turns
out to be 128 bits, say, use `int_least64_t' from <stdint.h>.
Still in C99, if you need *exactly* 64 bits you can use
`int64_t', also from <stdint.h>. However, C99 does not require
that this type be supported by all implementations, so you can't
count on it being available. It may be enough that your code
will fail to compile on such an implementation; the person
attempting to port it will at least be ade aware of the trouble.
The earlier C90 Standard did not define `long long' --
in fact, using `long long' in a program was a syntax error.
Still, many compilers provided it as an extension when
invoked in a non-strict mode. About the best you can do
is to hope that such compilers also "advertise" the fact
through the <limits.h> macros, so you can test at compile
time whether a suitable `long long' exists. Making the
test is just a bit tricky because the preprocessor may
not be able to handle such large values; here's one way:
#include <limits.h>
#if (LLONG_MAX >> 31) >> 31 == 1
typedef long long int64;
#else
#error "No 64-bit integer type!"
#endif
(Obviously, you could change `==' to `>=' if you need an
"at least" rather than an "exactly" 64-bit type.)
This isn't 100% certain to work, though. `LLONG_MAX'
is mandated by C99 but was (obviously) absent from C90,
and a compiler that decided to provide `long long' before
C99 appeared might not have described the type in exactly
the same way C99 would eventually require. Also, the
support for `long long' in things like printf() may not
be exactly what C99 eventually specified. Still, a test
like this is probably the best you can do for a pre-C99
implementation.
-- Eric.Sosman@sun.com
- Next message: uydo\(kiboga\): "how tp pass a stream into a structure"
- Previous message: Fao, Sean: "Re: writing tsr in c"
- In reply to: ESOJAY: "Re: Defining int64"
- Next in thread: Dan Pop: "Re: Defining int64"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|