This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Distinguishing between long and long long on an LP64 machine?
- From: "John (Eljay) Love-Jensen" <eljay at adobe dot com>
- To: Mark Dickinson <dickinsm at gmail dot com>, GCC-help <gcc-help at gcc dot gnu dot org>
- Date: Tue, 27 Oct 2009 05:32:25 -0700
- Subject: Re: Distinguishing between long and long long on an LP64 machine?
Hi Mark,
> If I'm using gcc on a machine where both long and long long also have 64 bits,
> is there any simple C99 (+ gcc extension) code that will allow me to figure
> out whether this type is ultimately defined as long or long long?
If you are using C++, yes. If you are using C... I don't think so.
For your example, this is how I do it:
off_t my_offset;
//...
printf("%lld", (long long)my_offset);
Why? Because there is no printf format specifier for off_t:
printf("%{off_t}d", my_offset);
And *that* is really the problem you are running into. (If I recall
correctly, C/C++ Users Journal had published an article on extensible printf
called eprintf, and there you could register off_t handling.)
If you are using C99, then you could do it this way:
#include <inttypes.h>
//...
off_t my_offset;
//...
printf("%"PRId64, (int64_t)my_offset);
That's even more port-robust. For the ultimate in future-protection
paranoia, you could add a static check to ensure that sizeof(off_t) ==
sizeof(int64_t). I think that's about as close as you can get, and it
doesn't distinguish between long int and long long if both are 64-bit.
> ... I'm still curious to know whether there's a simple way for a gcc *user* to
> distinguish between long and long long when both are 64 bits. typeof doesn't
> seem to help here. I guess I'm really after some imaginary extension
> 'rank_of' that lets me do
C? I don't think so.
C++? Yes, standard C++. (You can use C++ as a "better C". But these days,
that will probably get you scowls from C folks who eschew C++.)
Sincerely,
--Eljay