如何printf的64位整数(十六进制)?整数、printf、十六进制

由网友(随疯奔跑)分享简介:通过以​​下code我试图输出使用 unit64_t 变量的值的printf()。编译code与海湾合作委员会,将返回以下警告:With the following code I am trying to output the value of a unit64_t variable using printf()....

通过以​​下code我试图输出使用 unit64_t 变量的值的printf()。编译code与海湾合作委员会,将返回以下警告:

With the following code I am trying to output the value of a unit64_t variable using printf(). Compiling the code with gcc, returns the following warning:

警告:格式'%X'预计类型为无符号整型的说法,   但参数2的类型为uint64_t中'[-Wformat =]

warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 2 has type ‘uint64_t’ [-Wformat=]

在code:

#include <stdio.h>
#include <stdint.h>

int main ()
{
    uint64_t val = 0x1234567890abcdef;
    printf("val = 0x%xn", val);

    return 0;
}

输出:

val = 0x90abcdef

期望的输出:

Expected output:

val = 0x1234567890abcdef

我怎么能输出64位值用十六进制整数的printf()?该 X 说明似乎错在这种情况下。

How can I output a 64bit value as a hexadecimal integer using printf()? The x specifier seems to wrong in this case.

推荐答案

从你的编译器的警告,告诉你,你的格式说明没有你传递给它的数据类型相匹配。

The warning from your compiler is telling you that your format specifier doesn't match the data type you're passing to it.

尝试使用%LX %LLX 。欲了解更多便携性,包括 inttypes.h 并使用 PRIx64 宏。

Try using %lx or %llx. For more portability, include inttypes.h and use the PRIx64 macro.

例如:的printf(VAL =为0x%PRIx64 N,VAL); (注意,这是字符串连接)

For example: printf("val = 0x%" PRIx64 "n", val); (note that it's string concatenation)

阅读全文

相关推荐

最新文章