我刚刚开始阅读 C 并且有一个关于宏的问题。
如何打印一个 5 字节的整数值(恰好在宏中定义)?
例如:
#define MAX 0xdeadbeaf12
int main(){
printf(" 0x %2x \n", MAX);
}
此代码打印 adbeaf12 但不打印 deadbeaf12。
如何打印所有字节?
最佳答案
我更愿意对变量的大小做出明确的假设。下面使用标准(尽管经常未使用)宏来定义 64 位常量并将这样的量打印为十六进制值。
#include <stdio.h>
// Pull in UINT64_C and PRIx64 macros
#include <inttypes.h>
// Make this a 64-bit literal value
#define MAX UINT64_C(0xdeadbeaf12)
int main()
{
// Use PRIx64 which inserts the correct printf specifier for
// a hex format of a 64-bit value
printf(" 0x%" PRIx64 " \n", MAX);
}
关于c - 如何打印 5 字节整数值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2672963/