C:长长总是64位?

时间:2011-03-07 15:39:21

标签: c

如果我在我的代码中使用long longs,那么无论运行什么代码的机器,我是否可以100%保证它们将具有64位?

4 个答案:

答案 0 :(得分:14)

不,C99标准表示它将至少 64位。所以它可能比我想的更多。您可以使用int64_t类型,如果您需要64位总是假设您有stdint.h可用(C99中的标准)。

#include <stdint.h>
int64_t your_i64;

答案 1 :(得分:1)

您可以使用此

测试编译器是否符合预处理器中数字的C99
# if (~0U < 18446744073709551615U)
#  error "this should be a large positive value, at least ULLONG_MAX >= 2^{64} - 1"
# endif

这是有效的,因为所有无符号值(在预处理器中)都必须与uintmax_t的类型相同,因此0U的类型为uintmax_t~0U,{ {1}}和0U-1都是最大可表示的数字。

如果此测试有效,-1U实际上unsigned long long的可能性很高。

对于预处理阶段后的有效表达式,用真实类型进行测试

uintmax_t

这有同样的技巧,但使用后缀unsigned long long has_ullong_max[-1 + 2*((0ULL - 1) >= 18446744073709551615ULL)]; 确保具有ULL类型的常量。

答案 2 :(得分:0)

它们保证是64位的最小值。从理论上讲,它们可能更大(例如,128位),尽管我合理地说它们在当前可用的任何东西上只有64位。

答案 3 :(得分:0)

#if CHAR_BIT * sizeof (long long) != 64
   #pragma error "long long is not 64 bits"
#endif

或某些等价物。

基于评论:如果您想支持在预处理器中无法使用sizeof的编译器,请参阅此主题:

http://www.daniweb.com/forums/thread13553.html

这样的事情:

 char longlongcheck[(sizeof(long long) * CHAR_BIT) == 64]; // won't compile if the expression is 0.