I'm trying to use 64 bit integers in C, but am getting mixed signals as to whether it should be possible.
When I execute the printf:
printf("Size of long int:%d\nSize of long long int:%d\n\n",(int)sizeof(long int), (int)sizeof(long long int));
The response I get is:
Size of long int:4 Size of long long int:8
This makes me feel that a long long int has 8 bytes = 64 bits.
However, when I try to declare the following variables:
long long int a2 = 0x00004444;
long long int b2 = 0x000044440;
long long int c2 = 0x0000444400;
long long int d2 = 0x00004444000;
long long int e2 = 0x000044440000;
long long int f2 = 0x0000444400004;
long long int g2 = 0x00004444000044;
long long int h2 = 0x000044440000444;
long long int i2 = 0x0000444400004444;
The last 4 variables (f2,g2,h2,i2) give me the error message:
warning: integer constant is too large for ‘long’ type
I get the same result when I replace 'long long int' with 'int64_t'. I assume 'int64_t' was recognized, since it didn't generate any error messages of its own.
So, it appears my 8 byte long long int is really a 6 byte long long int, and I don't understand what I'm missing here. If it's any help, here is the information on my gcc compiler:
me@ubuntu:~$ gcc -v
Using built-in specs.
Target: i686-linux-gnu
Configured with: ../src/configure -v
--with-pkgversion='Ubuntu/Linaro 4.4.4-14ubuntu5'
--with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++
--prefix=/usr
--program-suffix=-4.4
--enable-shared
--enable-multiarch
--enable-linker-build-id
--with-system-zlib
--libexecdir=/usr/lib
--without-included-gettext
--enable-threads=posix
--with-gxx-include-dir=/usr/include/c++/4.4
--libdir=/usr/lib
--enable-nls
--with-sysroot=/ -
-enable-clocale=gnu
--enable-libstdcxx-debug
--enable-objc-gc
--enable-targets=all
--disable-werror
--with-arch-32=i686
--with-tune=generic
--enable-checking=release
--build=i686-linux-gnu
--host=i686-linux-gnu
--target=i686-linux-gnu
Thread model: posix
gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5)
If anyone knows how (or if) 64 bit integers are accessible to me, I'd really appreciate any help. Thanks....
Append ll suffix to hex digits for 64-bit (long long int), or ull suffix for unsigned 64-bit (unsigned long long)
Use
int64_t
, that portable C99 code.For printing:
Going against the usual good idea to appending
LL
.Appending
LL
to a integer constant will insure the type is at least as wide aslong long
. If the integer constant is octal or hex, the constant will becomeunsigned 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 specifieslong long
to be at least 64-bit. So by usingLL
, 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
output
Use
stdint.h
for specific sizes of integer data types, and also use appropriate suffixes for integer literal constants, e.g.: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.htmllong long int i2 = 0x0000444400004444LL;
Additionally, the the compiler is discarding the leading zeros, so
0x000044440000
is becoming0x44440000
, which is a perfectly acceptable 32-bit integer (which is why you aren't seeing any warnings prior tof2
).