有一点让我对常规c整数声明感到困惑的是,他们的名字很奇怪,“long long”是最糟糕的。我只为32位和64位计算机构建,所以我不一定需要库提供的可移植性,但是我喜欢每种类型的名称都是一个长度相似的单个词,并且大小没有歧义。我应该在32/64位机器上使用stdint.h整数类型吗?
// multiple word types are hard to read
// long integers can be 32 or 64 bits depending on the machine
unsigned long int foo = 64;
long int bar = -64;
// easy to read
// no ambiguity
uint64_t foo = 64;
int64_t bar = -64;
在32位和64位机器:
1)可以使用较小的整数例如int16_t比东西慢更高如int32_t?
2)如果我需要for循环运行10次,可以使用可以处理它的最小整数而不是典型的32位整数吗?
for (int8_t i = 0; i < 10; i++) {
}
3)每当我使用一个整数,我知道永远都不会是负的是它确定喜欢使用的签名版本,即使我并不需要额外的范围内提供?
// instead of the one above
for (uint8_t i = 0; i < 10; i++) {
}
4)是否可以安全使用typedef的类型从stdint.h包括
typedef int32_t signed_32_int;
typedef uint32_t unsigned_32_int;
编辑:两个答案都一样好,我真的不能向一个瘦,所以我只是选择较低的代表回答者
你有看看http://stackoverflow.com/questions/9834747/reasons-to-use-or-not-stdint? – Nic
[使用(或不)stdint的原因]的可能重复(http://stackoverflow.com/questions/9834747/reasons-to-use-or-not-stdint) –
他们是类似的,但我有一些具体问题如何实施那些在那篇文章中没有明确回答的文章。同样来自该帖子的最佳答案也会让你听起来像固定类型不会出错,但根据下面两个答案已经解决的问题,这不一定是正确的。 – YeOldeBitwise