在 long 和 int 都是 4 个字节的系统上,哪个是最好的,为什么?
typedef unsigned long u32;
或
typedef unsigned int u32;
注意:uint32_t不是一种选择
每个平台都有定义uint32_t
stdint.h
或其C++等效cstdint
。 请使用标准类型,而不是创建自己的类型。
- http://pubs.opengroup.org/onlinepubs/7999959899/basedefs/stdint.h.html
- http://www.cplusplus.com/reference/cstdint/
- http://msdn.microsoft.com/en-us/library/hh874765.aspx
两者之间的大小相同,因此仅取决于您的使用情况。如果需要存储十进制值,请使用 long。
这里有一个更好和完整的答案:https://stackoverflow.com/questions/271076/what-is-the-difference-between-an-int-and-a-long-in-c/271132
编辑:我不确定十进制和长,如果有人可以确认,谢谢。
你说标准uint32_t
不是一个选项,那么在32位机器上使用long
和int
都是正确的,我会说
typedef unsigned int u32;
稍微好一点,因为在两种流行的 64 位机器数据模型(LLP64 和 LP64)上,int
仍然是 32 位,而long
可能是 32 位或 64 位。查看 64 位数据模型