printf会导致未定义的行为吗? [英] Can printf result in undefined behavior?
本文介绍了printf会导致未定义的行为吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
int main()
{
unsigned int i = 12;
printf("%lu", i); // This yields a compiler warning
}
在32位平台上, code> printf 使用%lu
结果是垃圾吗?
On a 32 bit platform, does using printf
with an int using %lu
result in garbage?
推荐答案
只有语句32位平台并不意味着 int
和 long
都有32位,以及它们的 unsigned
。
Only the statement "32 bit platform" doesn't mean that int
and long
both have 32 bits, as well as their unsigned
counterparts.
如果 unsingned long
,为%lu
创建的时间长于 unsigned int
。
So yes, indeed this can happen if unsingned long
, what %lu
is made for, is longer than unsigned int
.
但是即使长度相等,类型也不兼容,所以正式是未定义的行为。
But even if the lengths are equal, the types are not compatible, so formally it is undefined behaviour.
这篇关于printf会导致未定义的行为吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文