打印短而不是int ...?什么是“%hd”? [英] Print a short instead of an int... ? What's up with "%hd"?

查看:185
本文介绍了打印短而不是int ...?什么是“%hd”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



将参数传递给VAL函数时,所有整数类型都是

提升,浮点数变为双精度数。因此,签名的短片将被提升为签署的int
。因此,我看不出你为什么要b $ b有任何理由:


printf("%hd",my_short);


而不是:


printf("%d",my_short);

Martin


When passing arguments to a VAL function, all integer types are
promoted, and floats become doubles. Therefore, signed short will be
promoted to signed int. Therefore I can''t see any reason why you''d
have:

printf( "%hd", my_short);

instead of:

printf( "%d", my_short);
Martin

推荐答案

Martin Wells写道:
Martin Wells wrote:

将参数传递给VAL函数时,所有整数类型

晋升,浮动成为双打。因此,签名的短片将被提升为签署的int
。因此,我看不出你为什么要b $ b有任何理由:


printf("%hd",my_short);


而不是:


printf("%d",my_short);
When passing arguments to a VAL function, all integer types are
promoted, and floats become doubles. Therefore, signed short will be
promoted to signed int. Therefore I can''t see any reason why you''d
have:

printf( "%hd", my_short);

instead of:

printf( "%d", my_short);



我认为目的是允许对printf()和scanf()使用相同的格式字符串

。 'h''在打印过程中不会增加任何内容,但会对扫描过程产生影响。

I believe that the purpose is to allow the use of the same format string
for printf() and scanf(). The ''h'' adds nothing during the printing
process, but makes a difference in the scanning process.


" James Kuyper Jr "写道:
"James Kuyper Jr." wrote:

>

Martin Wells写道:
>
Martin Wells wrote:

传递时VAL函数的参数,所有整数类型都是

提升,浮点数变为双精度数。因此,签名的短片将被提升为签署的int
。因此,我看不出你为什么要b $ b有任何理由:


printf("%hd",my_short);


而不是:


printf("%d",my_short);
When passing arguments to a VAL function, all integer types are
promoted, and floats become doubles. Therefore, signed short will be
promoted to signed int. Therefore I can''t see any reason why you''d
have:

printf( "%hd", my_short);

instead of:

printf( "%d", my_short);



我认为目的是允许对printf()和scanf()使用相同的格式字符串

。 'h''在打印过程中不会增加任何内容,但会对扫描过程产生影响。


I believe that the purpose is to allow the use of the same format string
for printf() and scanf(). The ''h'' adds nothing during the printing
process, but makes a difference in the scanning process.



它可能有所作为。考虑%hx当传递(短)-1,

在我的系统上打印ffff,而%x打印ffffffff。


-

+ --------------------- ---- + -------------------- + ----------------------- +

| Kenneth J. Brody | www.hvcomputer.com | #include |

| kenbrody / at\spamcop.net | www.fptech.com | < std_disclaimer.h |

+ ------------------------- + --------- ----------- + ----------------------- +

不要 - 邮寄给我:< mailto:Th ************* @ gmail.com>

It could make a difference. Consider "%hx" when passed (short)-1,
which on my system prints "ffff", whereas "%x" prints "ffffffff".

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don''t e-mail me at: <mailto:Th*************@gmail.com>


Kenneth:
Kenneth:

它可能会有所作为。考虑%hx当传递(短)-1,

在我的系统上打印ffff,而%x打印ffffffff。
It could make a difference. Consider "%hx" when passed (short)-1,
which on my system prints "ffff", whereas "%x" prints "ffffffff".



好​​的我可以看到以下情况如何:


(无符号)(短)-1 == UINT_MAX(也许是0xffff)

(短无符号)(短)-1 == USHRT_MAX(也许是0xffffffff)


....但它不是未定义的行为如果需要无符号整数类型,请提供带有签名

整数类型的printf,反之亦然?


如果我想打印无符号的最大值简短(当然不使用

USHRT_MAX),然后我会选择:


printf("%u",(unsigned)(short)未签名的)-1)


马丁


OK I can see how the following are true:

(unsigned)(short)-1 == UINT_MAX (maybe 0xffff)
(short unsigned)(short)-1 == USHRT_MAX (maybe 0xffffffff)

....but is it not undefined behaviour to supply printf with a signed
integer type when it expects an unsigned integer type, or vice versa?

If I wanted to print the max value of an unsigned short (without using
USHRT_MAX of course), then I''d go with:

printf("%u",(unsigned)(short unsigned)-1)

Martin


这篇关于打印短而不是int ...?什么是“%hd”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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