printf(“%d”, 1.0) 是否未定义?
According to section 4.9.6.1 of the C89 draft, %d is a character that specifies the type of conversion to be applied.
The word conversion implies, in my opinion, that printf("%d", 1.0)
is defined.
Please confirm or refute this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
转换是将语言值转换为该值的词汇表示形式。
你的理论是错误的;行为未定义。规范说(7.19.6.1p8 和 9,使用 C99 TC2):
和
The conversion is the conversion of a language value to a lexical representation of that value.
Your theory is wrong; behavior is undefined. The spec says (7.19.6.1p8 and 9, using C99 TC2):
And
Printf 是一个可变参数函数,因此无法进行转换。编译器只是安排将双精度数推入参数列表。 Printf 无法确定它是 double 与 int 与大象。结果?混乱。
Printf is a varargs function, so no conversion is possible. The compiler just arranges to push a double onto the arguments list. Printf has no way to find out that it's a double versus an int versus an elephant. Result? Chaos.
这里的“转换”一词是指将
int
(这是这里唯一可接受的参数类型)转换为由该int
的十进制表示形式组成的字符串。代码>.它与从其他类型(例如double
)到int
的转换无关。The word "conversion" here is referring to the conversion of an
int
(which is the only acceptable argument type here) to a string of characters that make of the decimal representation of thatint
. It has nothing to do with conversion from other types (such asdouble
) toint
.不确定它是正式未定义还是错误 - 但它是错误的!
Not sure if it's officially undefined or an error - but it's wrong!