日期:2014-05-16  浏览次数:20836 次

Linux下64位整数的使用问题
想在Linux(ubuntu kernel 2.6.21)下使用64位整数,有如下程序片断:

...
long long llNum = 0x1111222233334444; //(1)
...
printf("0x%x\n", llNum); //(2)
...

编译时提示(1)处"Warning: integer constant is too large for 'long' type"
运行(2)时显示0x33334444
即使把long long换成int64_t或者long long int也不行,到底怎样才能使用64位整数呢?


------解决方案--------------------
C/C++ code

/*-
 * file: int64.c
 * auth: mymtom
 * date: 2008-08-19
 */

#include <sys/types.h>
#include <stdio.h>

int
main(void)
{
        long long       nLL = 0x1111222233334444LL;
        int64_t         n64 = 0x5555666677778888LL;

        (void)printf("%llx\n", nLL);
        (void)printf("%llx\n", n64);

        return (0);
}

------解决方案--------------------
你用的就是64位,只不过你当32为打印出来。

要去掉warning:
man gcc
-Wlong-long
Warn if long long type is used. This is default. To inhibit the
warning messages, use -Wno-long-long. Flags -Wlong-long and
-Wno-long-long are taken into account only when -pedantic flag is
used.