printf中%hd的用处 [英] Usefulness of %hd in printf

查看:416
本文介绍了printf中%hd的用处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

printf是否有任何可能的情况,%hd导致%d导致不同的
结果,相应的参数类型为''short int''?

Is there any possible situation for printf where %hd causes a different
result to %d, and the corresponding argument was of type ''short int'' ?

推荐答案

Old Wolf写道:
Old Wolf wrote:

printf是否有任何可能的情况,其中%hd导致不同的

结果为%d,相应的参数类型为''short int''?
Is there any possible situation for printf where %hd causes a different
result to %d, and the corresponding argument was of type ''short int'' ?



想想scanf()系列函数。


当然,短片会像浮子一样促进加倍对于

同样的原因。但地址不会促进。因此,需要一个小于默认促销类型的

宽度说明符。

Think about the scanf() family of functions.

Sure, the short will promote just like floats promote to double for the
same reason. But addresses do not promote. Hence, the need for a
width specifier for ''smaller than default promotion'' types.


user923005写道:
user923005 wrote:

Old Wolf写道:
Old Wolf wrote:

printf是否有任何可能的情况,其中%hd导致不同的

结果为%d,相应的参数类型为''short int''?
Is there any possible situation for printf where %hd causes a different
result to %d, and the corresponding argument was of type ''short int'' ?



想想scanf()系列函数。


Think about the scanf() family of functions.



我在询问printf()系列。


你是说printf没有区别,但他们为%hd添加了

支持,试图让printf系列保持类似于

scanf系列?

I''m asking about the printf() family.

Are you saying that there is no difference in printf, but they added
support for %hd just to try and keep the printf family similar to the
scanf family?


Old Wolf写道:
Old Wolf wrote:

user923005写道:
user923005 wrote:

Old Wolf写道:
Old Wolf wrote:

printf是否存在任何可能的情况,其中%hd导致%d导致不同的
结果,且相应的参数类型为' 'short int''?
Is there any possible situation for printf where %hd causes a different
result to %d, and the corresponding argument was of type ''short int'' ?



想想scanf()系列函数。

Think about the scanf() family of functions.



我在询问printf()系列。


你是说printf没有区别,但是为了尝试保持printf系列类似于

scanf系列,他们为%hd添加了

支持?


I''m asking about the printf() family.

Are you saying that there is no difference in printf, but they added
support for %hd just to try and keep the printf family similar to the
scanf family?



不,我说scanf()必须拥有它,而printf()可以关心

更少。


我们可以轻易抱怨


12.9:有人告诉我,将%lf与printf()一起使用是错误的。如果scanf()需要%lf?


答:printf'确实如此
printf()使用%f作为double类型's%f说明符适用于float和

双参数。由于默认参数促销

(适用于可变长度参数列表,例如

printf',无论原型是否在范围内),

类型float的值被提升为double,而printf()因此看到

只增加一倍。 (printf()接受%Lf,长双。)

另见问题12.13和15.2。


参考文献:K& R1 Sec。 7.3 pp.145-47,Sec。 7.4页.147-50; K& R2

秒。 7.2 pp.153-44,Sec。 7.4 pp.157-59; ISO秒7.9.6.1,

秒。 7.9.6.2; H& S Sec。 15.8页,357-64,Sec。 15.11页.366-78;

CT& P Sec。 A.1 pp.121-33。

No. I am saying scanf() has to have it, while printf() could care
less.

We could just as easily complain about:

12.9: Someone told me it was wrong to use %lf with printf(). How can
printf() use %f for type double, if scanf() requires %lf?

A: It''s true that printf''s %f specifier works with both float and
double arguments. Due to the "default argument promotions"
(which apply in variable-length argument lists such as
printf''s, whether or not prototypes are in scope), values of
type float are promoted to double, and printf() therefore sees
only doubles. (printf() does accept %Lf, for long double.)
See also questions 12.13 and 15.2.

References: K&R1 Sec. 7.3 pp. 145-47, Sec. 7.4 pp. 147-50; K&R2
Sec. 7.2 pp. 153-44, Sec. 7.4 pp. 157-59; ISO Sec. 7.9.6.1,
Sec. 7.9.6.2; H&S Sec. 15.8 pp. 357-64, Sec. 15.11 pp. 366-78;
CT&P Sec. A.1 pp. 121-33.


这篇关于printf中%hd的用处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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