0

我在我的解决方案中包含了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。

4

4 回答 4

9

%u格式说明符printf用于无符号整数。使用%llu.

由于这是 C++,您不妨使用类型安全std::cout来避免程序员错误:

std::cout << test2;
于 2013-02-13T16:35:52.720 回答
3

利用:

#include <inttypes.h>

/* ... */

printf("%" PRIu64 "\n", test2);

打印一个uint64_t值。

u转换说明符用于打印一个unsigned int值。

请注意,该PRIu64宏是 C 宏。在 C++ 中,宏不存在,您可能必须使用%llu转换规范。

于 2013-02-13T16:36:20.963 回答
3

由于您还将其标记为 C++,因此我将添加一种明显的方法来避免类型不匹配,就像您遇到的那样printf:使用 ostream 代替:

std::cout << test2;
于 2013-02-13T16:38:09.567 回答
0

在我拥有的一本书中,它说程序在计算时总是将任何变量转换为整数。是否可以将默认整数设为 64 位?

这是1)不正确,2)不,你不能告诉(大多数)编译器int你想要什么大小。你当然可以使用类似的东西:

 typedef Int int64_t;
 typedef Uint uint64_t; 

但是你不能重命名int为编译器应该是的东西——可能是 16、32、64、36 或 72 位——或其他一些 >= 16 的数字。

于 2013-02-13T16:41:15.247 回答