8

If i'm using long longs in my code, can i absolutely 100% guarantee that they will have 64 bits no matter what machine the code is run on?

John
  • 111
  • 1
  • 1
  • 3

4 Answers4

15

No, C99 standard says that it will have at least 64 bits. So it could be more than that at some point I guess. You could use int64_t type if you need 64bits always assuming you have stdint.h available (standard in C99).

#include <stdint.h>
int64_t your_i64;
Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
  • `int64_t` must be provided if the implementation has a type that fits its requirements -- but it's (at least theoretically) possible that no such type exists, in which case `int64_t` won't be defined. – Jerry Coffin Mar 07 '11 at 15:50
  • `int_least64_t` **must be provided by C99 implementations**; this guarantees 64 or more bits – pmg Mar 07 '11 at 15:55
  • 1
    @pmg long long also guarantees **at least** 64 bits. That was the problem. :-) – Bo Persson Mar 07 '11 at 20:27
2

You can test if your compiler is C99 complying with respect to numbers in the preprocessor with this

# if (~0U < 18446744073709551615U)
#  error "this should be a large positive value, at least ULLONG_MAX >= 2^{64} - 1"
# endif

This works since all unsigned values (in the preprocessor) are required to be the same type as uintmax_t and so 0U is of type uintmax_t and ~0U, 0U-1 and -1U all are the maximum representable number.

If this test works, chances are high that unsigned long long is in fact uintmax_t.

For a valid expression after the preprocessing phase to test this with the real types do

unsigned long long has_ullong_max[-1 + 2*((0ULL - 1) >= 18446744073709551615ULL)];

This does the same sort of trick but uses the postfix ULL to be sure to have constants of type unsigned long long.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
0

They are guaranteed to be a minimnum of 64 bits. It's theoretically possible that they could be larger (e.g., 128 bits) though I'm reasonably they're only 64 bits on anything currently available.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • 2
    On llp64 memory model compilers, it would have been convenient if long long int was bumped up to 128 bits. – Chris Becke Mar 07 '11 at 15:43
  • I'd be surprised if no DSP with an non power of two size word is available and DSP have usually a C compiler. And then historical machines are a little (not much) more alive than often though: PDP-10 (36 bits processors, it has a port of gcc somewhere on the net and I think it used 72 bit long long) were still used as embedded processor in some network hardware last time I've looked at, and Unisys is still selling 36 bits and 48 bit machines -- I don't know if they have a C compilers for them or not. – AProgrammer Mar 07 '11 at 16:00
  • @AProgrammer: I believe Unisys has a C compiler, but at least the last time I looked, it only attempted to conform with C90 rather than C99, so I'm not at all sure it had long-long at all. I suppose my wording was ambiguous: while gcc is certainly available, PDP-10s haven't been for quite a while. – Jerry Coffin Mar 07 '11 at 16:03
  • 1
    @Jerry, If I was not clear, the practical cases I'd care of would be DSP, not PDP-10 (XKL still sold them in 1997 and I think they still are using them internally) nor Unisys machines. – AProgrammer Mar 07 '11 at 16:24
  • 1
    @AProgrammer: I'm less certain about DSPs. AD SigmaDSP, for example, are 28/56-bit processors, so the obvious choice for them would be 112 bits for `long long`, but at least the last time I used them, they didn't provide that. TigerSHARC use 64-bit long longs. I believe TI C5000 and C6000 use 64-bit long long as well. There are other vendors, but... – Jerry Coffin Mar 07 '11 at 17:19
0

With

#if CHAR_BIT * sizeof (long long) != 64
   #pragma error "long long is not 64 bits"
#endif

or some equivalent.

Based on comment: if you want to support compilers where sizeof can't be used in the pre-processor, see this thread:

http://www.daniweb.com/forums/thread13553.html

Something like this:

 char longlongcheck[(sizeof(long long) * CHAR_BIT) == 64]; // won't compile if the expression is 0.
Lou Franco
  • 87,846
  • 14
  • 132
  • 192
  • 4
    You can't use `sizeof` in a preprocessor expression (officially anyway -- some compilers allow it, but it's an extension). – Jerry Coffin Mar 07 '11 at 15:48
  • 1
    For those compilers there's a trick with making an array with the size that is illegal if it doesn't match. Will find and post. – Lou Franco Mar 07 '11 at 15:52
  • tricks with `sizeof` are unfortunately not guaranteed to work on bizarre machines with padding bits. See my answer for variants that don't depend on this. – Jens Gustedt Mar 07 '11 at 16:40
  • Umm, `#if LLONG_MIN != -0x7FFFFFFFFFFFFFFF-1` is the easy and simple way (this also checks for twos complement at the same time, which you'll almost surely want to ensure if you care about the number of bits). – R.. GitHub STOP HELPING ICE Mar 07 '11 at 17:11