英文:
Is "%#x" equivalent to "0x%x" in the format string of printf family functions?
问题
这两个函数调用等效吗?(假设变量 x
的类型为 unsigned int
)
printf ("%#x\n", x);
printf ("0x%x\n", x);
以及以下两个函数调用:
printf ("%#.8x\n", x);
printf ("0x%08x\n", x);
为什么一些嵌入式程序偏向于使用后者而不是前者?
英文:
Are these two function calls equivalent? ( Assuming that the type of variable x
is unsigned int
)
printf ("%#x\n", x);
printf ("0x%x\n", x);
And the following two function calls:
printf ("%#.8x\n", x);
printf ("0x%08x\n", x);
Why do some embedded programs prefers the latter one to the former one?
答案1
得分: 2
从C标准7.9.16.1第6段中关于fprintf
中的#
:
结果被转换为“替代形式”...对于x(或X)转换,非零结果前缀为0x(或0X)。
所以,%#x
将在非零结果前缀为 0x
。所以它与 0x%x
相同,除非结果为零。
英文:
From the C standard 7.9.16.1 para 6, on #
in fprintf
:
> The result is converted to an ‘‘alternative form’’... For x (or X) conversion, a nonzero
> result has 0x (or 0X) prefixed to it.
So there you have it: %#x
prefixes 0x
to any non-zero result. So it's the same as 0x%x
except when the result is zero.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论