c - 请解释下面 printfs 的区别

标签 c printf format-specifiers

printf("%x\n",(const uint8_t *)0x0D);
printf("%x\n",(const uint8_t)0x0D);
printf("%x\n",(uint8_t *)0x0D);
printf("%x\n",0x0D);

他们都给我D。这里的 const* 有何意义?

最佳答案

%x 格式说明符将参数指定为 unsigned int 类型。

就您而言,

 printf("%x\n",(const uint8_t)0x0D);
 printf("%x\n",0x0D);

参数将被提升(默认提升规则)以匹配类型,但在这种情况下

 printf("%x\n",(const uint8_t *)0x0D);  //supplying a pointer
 printf("%x\n",(uint8_t *)0x0D);        //supplying a pointer

您将调用undefined behavior ,按照 C11 第 §7.21.6.1 章

[...] If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined. [...]

关于c - 请解释下面 printfs 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36909273/

相关文章:

c - 为什么堆栈上溢/下溢不会触发运行时错误?

c - 使用输出以下程序的循环编写程序

c - 在 C 中使用 scanf() 错误地写入文件

c - Linux : printing a zero-padded string of specified length

c - 如何将 vasprintf() 与特殊字符 (%) 一起使用?

C - Linux 内核 - 如何使用 timespec_to_ns()?

c# - 线性分开两组

php - Openssl 和 Windows CryptoAPI 兼容性问题

c - 如何使用 fprintf 并写入管道?

c - 格式说明符的类型 channel