在printf中奇怪地使用格式说明符 [英] strange use of format specifier in printf

查看:75
本文介绍了在printf中奇怪地使用格式说明符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此帖子中有一个
http://groups.google.co.in/group/com...b0dbf76f3e02e3


Tydr Schnubbis在第三次回复中使用%d来打印char'的...是不是

错了?

Here in this thread
http://groups.google.co.in/group/com...b0dbf76f3e02e3

Tydr Schnubbis in 3rd reply used %d for printing char''s...isn''t it
wrong?

推荐答案



Money写道:

Money wrote:
这个帖子在这个帖子中
http://groups.google。 co.in/group/com...b0dbf76f3e02e3

Tydr Schnubbis在第3回复中使用了%d来打印字符'...不是吗
错误?
Here in this thread
http://groups.google.co.in/group/com...b0dbf76f3e02e3

Tydr Schnubbis in 3rd reply used %d for printing char''s...isn''t it
wrong?




不会。它会以小数形式打印出来,而不是字符。


我建议当你作为

参数传递时,你会查看char被提升的内容。


Tom



No. It will print them in decimal though, not as characters.

I suggest you look up what char is promoted to when passed as an
argument.

Tom



" Money" < SP ********* @ gmail.com>在消息中写道

news:11 ********************* @ m73g2000cwd.googlegro ups.com ...

"Money" <sp*********@gmail.com> wrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
在这个帖子中
http://groups.google.co.in/group/com...b0dbf76f3e02e3

Tydr Schnubbis在第3回复中使用%d来打印字符'...是不是错了?
Here in this thread
http://groups.google.co.in/group/com...b0dbf76f3e02e3

Tydr Schnubbis in 3rd reply used %d for printing char''s...isn''t it
wrong?



是的。这必须是对超链接的假设,而OP

需要时间来量身定制他对论坛的论点。 ciao,f


Yes. This has to be the presumption against a hyperlink versus the OP
taking time to tailor his argument to the forum. ciao, f


Money写道:
这里有一个帖子
http://groups.google .co.in / group / com ... b0dbf76f3e02e3

Tydr Schnubbis在第3次回复时使用了%d来打印char'的...不是吗
错误?
Here in this thread
http://groups.google.co.in/group/com...b0dbf76f3e02e3

Tydr Schnubbis in 3rd reply used %d for printing char''s...isn''t it
wrong?




在带有签名''char''类型的平台上,当''char''值传递为

参数时'''''(省略号)参数,它们首先被提升为''int''

值。所以它实际上是''int''值。使用带有''int''值的''%d''格式说明符没有任何错误




在无符号平台上''char''类型,''int''可能不大

足以容纳''char''的所有值,''char''将被提升为'''未签名

int''而不是。在这种特殊情况下,代码会导致未定义的行为,因为使用''%d''格式说明符和''unsigned int''值是非法的。

-

祝你好运,

Andrey Tarasevich



On a platform with signed ''char'' type, when ''char'' values are passed as
arguments for ''...'' (ellipsis) parameters, they are first promoted to ''int''
values. So it''s ''int'' values that are actually passed. And there''s nothing wrong
with using ''%d'' format specifier with ''int'' values.

On a platform with unsigned ''char'' type, it is possible that ''int'' is not large
enough to hold all values of ''char'' and ''char'' will be promoted to ''unsigned
int'' instead. In this particular case the code would lead to undefined behavior,
since it is illegal to use ''%d'' format specifier with ''unsigned int'' values.

--
Best regards,
Andrey Tarasevich


这篇关于在printf中奇怪地使用格式说明符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆