468,556 Members | 2,395 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,556 developers. It's quick & easy.

Defining int64

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

Thanks,
ESOJAY

--
ESOJAY
Nov 13 '05 #1
6 40000

"ESOJAY" <es******@hotmail.com> wrote in message
news:bp**********@news.ox.ac.uk...
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
Nov 13 '05 #2
Thanks.
Unfortunately, I am constrained to use MIPSpro C compiler, which dosen't
seem to understand _int64 or __int64.

ESOJAY
"Tom St Denis" <to********@iahu.ca> wrote in message
news:9T************@news04.bloor.is.net.cable.roge rs.com...

"ESOJAY" <es******@hotmail.com> wrote in message
news:bp**********@news.ox.ac.uk...
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

Nov 13 '05 #3

"ESOJAY" <es******@hotmail.com> wrote in message
news:bp**********@news.ox.ac.uk...
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
Nov 13 '05 #4

"Tom St Denis" <to********@iahu.ca> wrote in message
news:Eo*************@news04.bloor.is.net.cable.rog ers.com...

"ESOJAY" <es******@hotmail.com> wrote in message
news:bp**********@news.ox.ac.uk...
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


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
Nov 13 '05 #5
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.

--
Er*********@sun.com
Nov 13 '05 #6
In <bp**********@news.ox.ac.uk> "ESOJAY" <es******@hotmail.com> writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Lau Lei Cheong | last post: by
14 posts views Thread by cj | last post: by
12 posts views Thread by Allen | last post: by
3 posts views Thread by Tim Sprout | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by UniDue | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.