How to specify 64 bit integers in c
Use stdint.h
for specific sizes of integer data types, and also use appropriate suffixes for integer literal constants, e.g.:
#include <stdint.h>
int64_t i2 = 0x0000444400004444LL;
Try an LL
suffix on the number, the compiler may be casting it to an intermediate type as part of the parse. See http://gcc.gnu.org/onlinedocs/gcc/Long-Long.html
long long int i2 = 0x0000444400004444LL;
Additionally, the the compiler is discarding the leading zeros, so 0x000044440000
is becoming 0x44440000
, which is a perfectly acceptable 32-bit integer (which is why you aren't seeing any warnings prior to f2
).
Use int64_t
, that portable C99 code.
int64_t var = 0x0000444400004444LL;
For printing:
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
printf("blabla %" PRIi64 " blabla\n", var);
How to specify 64 bit integers in c
Going against the usual good idea to appending LL
.
Appending LL
to a integer constant will insure the type is at least as wide as long long
. If the integer constant is octal or hex, the constant will become unsigned long long
if needed.
If ones does not care to specify too wide a type, then LL
is OK. else, read on.
long long
may be wider than 64-bit.
Today, it is rare that long long
is not 64-bit, yet C specifies long long
to be at least 64-bit. So by using LL
, in the future, code may be specifying, say, a 128-bit number.
C has Macros for integer constants which in the below case will be type int_least64_t
#include <stdint.h>
#include <inttypes.h>
int main(void) {
int64_t big = INT64_C(9223372036854775807);
printf("%" PRId64 "\n", big);
uint64_t jenny = INT64_C(0x08675309) << 32; // shift was done on at least 64-bit type
printf("0x%" PRIX64 "\n", jenny);
}
output
9223372036854775807
0x867530900000000