c - 如何在 c 中指定 64 位整数

标签 c 64-bit sizeof

我正在尝试在 C 中使用 64 位整数,但关于它是否应该可行,我得到了混合信号。

当我执行 printf 时:

printf("Size of long int:%d\nSize of long long int:%d\n\n",(int)sizeof(long int), (int)sizeof(long long int));

我得到的响应是:

长整数的大小:4 long long int的大小:8

这让我觉得long long int有8个字节= 64位。

但是,当我尝试声明以下变量时:

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;

最后 4 个变量 (f2,g2,h2,i2) 给我错误信息:

警告:整数常量对于“long”类型来说太大了

当我将“long long int”替换为“int64_t”时,我得到了相同的结果。我假设“int64_t”已被识别,因为它自己没有生成任何错误消息。

所以,看来我的 8 字节 long long int 实际上是 6 字节 long long int,我不明白我在这里遗漏了什么。如果有任何帮助,这里是我的 gcc 编译器的信息:

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)   

如果有人知道我如何(或是否)可以访问 64 位整数,我将非常感谢任何帮助。谢谢....

最佳答案

对特定大小的整数数据类型使用stdint.h,并为整数文字常量使用适当的后缀,例如:

#include <stdint.h>

int64_t i2 = 0x0000444400004444LL;

关于c - 如何在 c 中指定 64 位整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9606455/

相关文章:

c++ - 使用带有 C 函数指针的 C++ 类成员函数

bash - 64 位 Ubuntu 上的脚本问题

c - 为什么在 32 位系统中包含一个 int64 变量时结构大小是 8 的倍数

c++ - 内存去了哪里?

c - 使用代码块无法读取字符

C 回文程序可以在本地 PC 上运行,但不能在 INGInious 上运行

process - 为什么 execve 的 argv 和 envp 参数不指向 const?

python - 在 Python 3.5 64 位上通过 pip 安装 OpenCV

.net - SHA256Managed 在 x64 构建中的速度是原来的两倍——这是典型的吗?

c - 如何找到数组中元素的数量?