我现在脑子里有两个问题:
首先,为什么我们需要int32_t,因为我们已经有了不同的变体,比如short int unsigned int等。
其次,使用这种类型的固定大小类型可以使程序可移植吗?
答案 0 :(得分:2)
- 首先,为什么我们需要int32_t,因为我们已经有了不同的变体,比如short int unsigned int等。
醇>
因为short int
,unsigned int
等在架构之间无法移植。
如果你的意思是32位,那就说明一点。否则,您可能最终只使用具有不同CPU架构的unsigned int
使用64位。
- 其次使用这种固定大小的类型可以使程序可移植吗?
醇>
是的,如上所述。
答案 1 :(得分:1)
1)int32_t提供精确的32位整数。这很重要,因为您可以将应用程序移植到不同的平台而无需重写算法(如果它们将编译,是的,int不总是16或32或64位宽,请检查C参考)。查看nice self-explanatory page about stdint.h types
2)可能,是的
答案 2 :(得分:-1)
不要被发现,始终使用 stdint.h 中提供的定义来定义特定位大小的整数。
考虑一下。
在 gcc 中 int32_t 定义为:
typedef signed int int32_t;
但 unsigned int 默认为 64 位长。
#include <stdio.h>
#include <stdint.h>
int main(int argc, char *argv[])
{
printf("sizeof(signed int) = %d\n", sizeof(signed int));
printf("sizeof(int32_t) = %d\n", sizeof(int32_t));
}
产生这个输出:
sizeof(signed int) = 8
sizeof(int32_t) = 4
那么有符号整数是 8 字节还是 4 字节?当 int32_t 是 signed int 的 typedef 时,为什么 int32_t 的长度与 signed int 不同?