双printf格式...... [英] Double printf format...

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

问题描述

在下面的程序中


#include< stdio.h>

int main()

{

int id = 123,io = 0123,ih = 0x123;

long ld = 1234567L,lo = 01234567l,lh = 0X1234567L;

float f = 123.456f;

double d = 01234.56789L;


printf(" id =%d \ t%o \ t%x \ nio =%d \ t%\\\ t%x \ nhh =%d \ t%O \%x \ n",

id,id,id,io, io,io,ih,ih,ih);

printf(" ld =%ld \ t%lo \ t%lx \ nlo =%ld \ t%lo \ t%lx \ nlh =%ld \ t%lo \ t%l \ n",

ld,ld,ld,lo,lo,lo,lh,lh,lh) ;

printf(" f =%lf \ t%le \ t%lg \ n =%lf \ t%lE \ t%lG \ n,f, f,f,d,d,d);

返回0;

}


输出得到:

id = 123 173 7b

io = 83 123 53

ih = 291%O 123

ld = 1234567 4553207 12d687

lo = 3 42391 1234567 53977

lh = 19088743 110642547 1234567

f = 123.456001 1.234560e + 02 123.456

d = 1234.567890 1.234568E + 03 1234.57


为什么第3行中有%O而不是预期的八进制o / p?

并且对于d而言,最后一个o / p是1234.57。但%g是%lf或%le

对吗?所以如果它是%lf那么为什么它是1234.57而不是1234.56789?


谢谢

in program below

#include <stdio.h>
int main()
{
int id = 123, io = 0123, ih = 0x123;
long ld = 1234567L, lo = 01234567l, lh = 0X1234567L;
float f = 123.456f;
double d = 01234.56789L;

printf("id = %d\t%o\t%x\nio = %d\t%o\t%x\nih = %d\t%O\t%X\n",
id, id, id, io, io, io, ih, ih, ih);
printf("ld = %ld\t%lo\t%lx\nlo = %ld\t%lo\t%lx\nlh = %ld\t%lo\t%l\n",
ld, ld, ld, lo, lo, lo, lh, lh, lh);
printf("f = %lf\t%le\t%lg\nd = %lf\t%lE\t%lG\n", f, f, f, d, d, d);
return 0;
}

the output got is:
id = 123 173 7b
io = 83 123 53
ih = 291 %O 123
ld = 1234567 4553207 12d687
lo = 342391 1234567 53977
lh = 19088743 110642547 1234567
f = 123.456001 1.234560e+02 123.456
d = 1234.567890 1.234568E+03 1234.57

why is there a %O in line 3 instead of octal o/p expected?
and in line for d the last o/p is 1234.57. but %g is either %lf or %le
right? so if it is %lf then why is it 1234.57 instead of 1234.56789?

thanks

推荐答案

naunetr写道:
naunetr wrote:

以下程序


#include< stdio.h>

int main()

{

int id = 123,io = 0123,ih = 0x123;

long ld = 1234567L,lo = 01234567l, lh = 0X1234567L;

float f = 123.456f;

double d = 01234.56789L;


printf(" id = %d \%%o \ t%x \ nn =%d \ t%\\\%x \ nhh =%d \ t%O \%x \ n",

id,id,id,io,io,io,ih,ih,ih);

printf(" ld =%ld \ t%lo \ t %lx \ nlo =%ld \ t%lo \ t%lx \ nlh =

%ld \ t%lo \ t%l \ n",

ld,ld,ld,lo,lo,lo,lh,lh,lh);

printf(" f =%lf \ t%le\t%lg \ n =%lf \ t%lE \ t%lG \
",F,F,F,d,d,

d);返回0;

}


输出得到:

id = 123 173 7b

io = 83 123 53

ih = 291%O 123

ld = 1234567 4553207 12d687

lo = 342391 1234567 53977

lh = 19088743 110642547 1234567

f = 123.456001 1.234560e + 02 123.456

d = 1234.567890 1.234568E + 03 1234.57


为什么第3行有%O而不是预期的八进制o / p?
in program below

#include <stdio.h>
int main()
{
int id = 123, io = 0123, ih = 0x123;
long ld = 1234567L, lo = 01234567l, lh = 0X1234567L;
float f = 123.456f;
double d = 01234.56789L;

printf("id = %d\t%o\t%x\nio = %d\t%o\t%x\nih = %d\t%O\t%X\n",
id, id, id, io, io, io, ih, ih, ih);
printf("ld = %ld\t%lo\t%lx\nlo = %ld\t%lo\t%lx\nlh =
%ld\t%lo\t%l\n",
ld, ld, ld, lo, lo, lo, lh, lh, lh);
printf("f = %lf\t%le\t%lg\nd = %lf\t%lE\t%lG\n", f, f, f, d, d,
d); return 0;
}

the output got is:
id = 123 173 7b
io = 83 123 53
ih = 291 %O 123
ld = 1234567 4553207 12d687
lo = 342391 1234567 53977
lh = 19088743 110642547 1234567
f = 123.456001 1.234560e+02 123.456
d = 1234.567890 1.234568E+03 1234.57

why is there a %O in line 3 instead of octal o/p expected?



八进制输出的格式说明符是%o,而不是%O。在C情况下,

很重要。

The format specifier for octal output is %o, not %O. In C case is
important.


并且对于d,最后一个o / p是1234.57。但%g是%lf或%le

对吗?所以如果它是%lf那么为什么它是1234.57而不是1234.56789?
and in line for d the last o/p is 1234.57. but %g is either %lf or %le
right? so if it is %lf then why is it 1234.57 instead of 1234.56789?



我认为这是一个精确的问题。

I think that''s a precision issue.


naunetr写道:

[...]
naunetr wrote:
[...]

为什么有一个%O在行预期3而不是八进制o / p?
why is there a %O in line 3 instead of octal o/p expected?



因为你把它放在那里。格式字符串的相关片段是

ih =%d \ t%O \ t%X \ n


注意%O in上面的字符串?没有%O转换说明符,

和八进制转换说明符是%o

Because you put it there. The relevant fragment of the format string is
ih = %d\t%O\t%X\n

Notice the %O in the string above? There is no %O conversion specifier,
and the "octal" conversion specifier is %o



谢谢Lew。是否有一个说明符,它会在八进制前面加一个0

数?喜欢0x为%#x?


thanks Lew. is there a specifier which will put a 0 in front off the octal
number? like 0x for %#x?



0%o怎么样?和0x%x?


[...]


-

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

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

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

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

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

What about "0%o" and "0x%x"?

[...]

--
+-------------------------+--------------------+-----------------------+
| 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>


文章< fr**********@registered.motzarella.org>,

naunetr< wi ************ @ roadtonowhere.invalidwrote:
In article <fr**********@registered.motzarella.org>,
naunetr <wi************@roadtonowhere.invalidwrote:

>谢谢Lew。是否有一个说明符,它会在八进制数字前面加一个0?喜欢0x为%#x?
>thanks Lew. is there a specifier which will put a 0 in front off the octal
number? like 0x for %#x?



%#o


C89 4.9.6.1 fprintf功能


#结果将转换为替代形式。对于o

转换,它会增加精度以强制结果的第一个数字
为零。对于x(或X)转换,

非零结果将以0x(或0X)作为前缀。 [...]

%#o

C89 4.9.6.1 The fprintf Function

# The result is to be converted to an "alternate form." For o
conversion, it increases the precision to force the first digit
of the result to be a zero. For x (or X) conversion, a
nonzero result will have 0x (or 0X) prefixed to it. [...]


>抱歉让它与scanf混淆了!但为什么还有%F?难道不应该足够吗?
>sorry got it confused with scanf! but why is there a %F too? shouldn''t %f
be enough?



C89没有%F(但确实有%E和%G)。 %E和%G使用

资本E代表指数代号,而不是小写e />
,但分别与%e和%g相同。 br />
-

我们观察到的怪癖和随意性迫使我们得出结论,即我们不是唯一的宇宙。 - Walter Kistler

C89 does not have %F (but does have %E and %G). %E and %G use a
capital E for the exponent signifier instead of a lower-case e
but are otherwise the same as %e and %g respectively.
--
"The quirks and arbitrariness we observe force us to the
conclusion that ours is not the only universe." -- Walter Kistler


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

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