Defining int64

E

ESOJAY

Is "typedef long long int64" the best way to define an int64 type in c? Are
there better alternatives?

Thanks,
ESOJAY
 
T

Tom St Denis

ESOJAY said:
Is "typedef long long int64" the best way to define an int64 type in c? Are
there better alternatives?

I use this in my code

#if defined(_MSC_VER) || defined(__BORLANDC__)
typedef unsigned __int64 ulong64;
typedef signed __int64 long64;
#else
typedef unsigned long long ulong64;
typedef signed long long long64;
#endif

It works for GCC, MSVC and BorlandC which will cover the vast majority of
platforms.

Tom
 
E

ESOJAY

Thanks.
Unfortunately, I am constrained to use MIPSpro C compiler, which dosen't
seem to understand _int64 or __int64.

ESOJAY
 
T

Tom St Denis

ESOJAY said:
Thanks.
Unfortunately, I am constrained to use MIPSpro C compiler, which dosen't
seem to understand _int64 or __int64.

It doesn't allow "long long"? Then tell the MIPSpro C compiler guys to
update their compiler :)

Or use GCC...

Tom
 
E

ESOJAY

Tom St Denis said:
It doesn't allow "long long"? Then tell the MIPSpro C compiler guys to
update their compiler :)

Or use GCC...

Tom

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.

ESOJAY
 
D

Dan Pop

In said:
Is "typedef long long int64" the best way to define an int64 type in c? Are
there better alternatives?

long long is an adequate definition for a 64-bit integer on all the
compilers supporting this type that I'm aware of. However, compilers
that do not originate under Unix have a tendency to use a different name
for their 64-bit integer types (if they support them at all). And, of
course, I/O on such types is performed in a platform specific way.

If your C89 compiler supports long long as an extension, you can no longer
invoke the compiler in conforming mode if you use it. Depending on your
purposes, this may or may not be an issue.

The advantage of names like __int64 is that you can use them even when
invoking the compiler in conforming mode, without requiring a diagnostic.

No such problems for C99 compilers (long long is a standard C99 type),
but they are few and far between.

Dan
 
E

Eric Sosman

ESOJAY said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top