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?
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. Save this answer.
The minimum size for char is 8 bits, the minimum size for short and int is 16 bits, for long it is 32 bits and long long must contain at least 64 bits.
In C and C++, there are four different data type available for holding the integers i.e., short, int, long and long long.
LLP64 or 4/4/8 ( int and long are 32-bit, pointer is 64-bit)
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;
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With