[英]I want to use the full range that the 64-bit integer can take. How can I do it in x86 Visual Studio 2008?
我在解决方案中包含了stdint.h并使用了uint64_t ,但结果不是我想要的。 这是我使用的代码。
#include <stdio.h>
#include "./stdint.h"
void main (void)
{
//-- to get the maximum value that the 32-bit integer can take
unsigned int test = 0 ;
test-- ;
//-- to see if the 64-bit integer can take the value that the 32-bit integer can't take
uint64_t test2 = test ;
test2++ ;
printf("%u\n", test) ;
printf("%u\n", test2) ;
while(1) { }
}
这就是结果。
4294967295
0
我想使用64位整数可以接受的全部范围。 如何在x86 Visual Studio 2008中做到这一点? 供您参考,我使用的是32位Windows 7。
printf
的%u
格式说明符用于无符号整数。 使用%llu
。
由于这是C ++,因此您最好使用类型安全的std::cout
以避免程序员错误:
std::cout << test2;
采用:
#include <inttypes.h>
/* ... */
printf("%" PRIu64 "\n", test2);
打印一个uint64_t
值。
u
转换指定用于打印的unsigned int
值。
请注意, PRIu64
宏是C宏。 在C ++中,该宏不存在,您可能必须使用%llu
转换规范。
由于您也将其标记为C ++,因此我将添加一种避免类型不匹配的明显方法,就像您在printf
遇到的那样:改用ostream:
std::cout << test2;
在我有一本书中,它说程序在计算时总是将任何变量转换为整数。 是否可以将默认整数设置为64位?
这是1)不正确,并且2)不,您无法告诉(大多数)编译器所需的int
大小。 您当然可以使用类似:
typedef Int int64_t;
typedef Uint uint64_t;
但是您不能将int
重命名为编译器应具有的其他名称-可能是16、32、64、36或72位-或其他一些数字== 16。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.