printf异常 [英] printf anomaly

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

问题描述

嗨专家


考虑这段代码


unsigned long data = 124; / *数据实际上是一个大结构的一部分,但

简化* /

printf("%-5llu",(unsigned long long)(数据* 1000) ));

<内存地址说60000000>:00000000 00000D77 00000000

00000000

0xD77是3447


假设数据位于内存地址60000000的开头,这是
a大端机器(基于MIPS)。

编译器从60000000执行两个加载字然后将60000000 + 4和

乘以1000再打印3447000,这不是预期的b $ b。


我的理解是1000将默认为int

然后它将被提升为unsigned long(因为在我们的平台上很长

和int具有相同的大小)

然后操作将以unsigned long进行(结果我的意思是)。

然后* result *将被转换为unsigned long long然后它将是

通过printf

(不知道怎么做 - 这是MIP基于S的平台)和printf是一个可变的

函数。


我必须丢失一些东西或编译器坏了吗?


1.请帮我理解发生了什么。

2.我怎样才能以正确的方式修复它。


请指教。

提前致谢,

Hi experts

Consider this piece of code

unsigned long data = 124; /* Data is actually part of a big struct but
to simplify */
printf(" %-5llu ", (unsigned long long)(data*1000));
<memory address say 60000000>: 00000000 00000D77 00000000
00000000
0xD77 is 3447

Assume data is at the beginning of memory address 60000000 and this is
a big endian machine (MIPS based).
The compiler is doing two load word from 60000000 and 60000000+4 and
then multiplying it by 1000 and then prints 3447000 which is not
expected.

My understanding is that the 1000 will default to int
Then it will be promoted to unsigned long (since on our platform long
and int are of same size)
then operation will be carried out in unsigned long (result I mean).
then *result* will be cast to unsigned long long and then it will be
passed to printf
(not sure how - this is MIPS based platform) and printf is a variadic
function.

I must be missing something or the compiler is broken?

1.Please help me understand what is going on.
2.And how can I fix it the right way.

Please advise.
Thanks in advance,

推荐答案


unsigned long data = 0x124
unsigned long data = 0x124



请忽略上一篇文章中的上述初始化行。

我只想说:数据类型是无符号的很长。

谢谢

please ignore the above initialization line in previous post.
I only wanted to say : the type of data is unsigned long.
Thanks


在******* @ yahoo.com 中写道:
in*******@yahoo.com wrote:

>
>

unsigned long data = 0x124
unsigned long data = 0x124



请忽略上一篇文章中的上述初始化行。


please ignore the above initialization line in previous post.



该行不在您之前的帖子中。

That line wasn''t in your previous post.


我只想说:数据类型是无符号长的。
I only wanted to say : the type of data is unsigned long.



您需要说的不仅仅是数据的类型。

投诉的性质是printf的输出,

也取决于数据的价值。


创建并发布仍然存在问题的最小程序。

尝试使用复制和粘贴设施或类似的东西,

而不是发布你的回忆

的代码可能看起来像。


-

pete

You do need to say more than the type of the data.
The nature of your complaint is the output of printf,
which also depends on the value of data.

Create and post the smallest program that still exhibits your problem.
Try to use copy and paste facilities or something equivalent,
instead of posting you recollections
of what the code may have looked like.

--
pete


** *****@yahoo.com 写道:

考虑这段代码


unsigned long data = 124; / *数据实际上是一个大结构的一部分,但

简化* /

printf("%-5llu",(unsigned long long)(数据* 1000) ));


<内存地址说60000000>:00000000 00000D77 00000000

00000000

0xD77是3447


假设数据位于内存地址60000000的开头,这是
a大端机器(基于MIPS)。

编译器正在做两个加载来自60000000和60000000 + 4以及

的单词然后乘以1000然后打印3447000,这不是预期的



我的理解是1000将默认为int

然后它将被提升为unsigned long(因为在我们的平台上很久

和int具有相同的大小)

然后操作将以unsigned long进行(结果我的意思是)。

然后* result *将被转换为unsigned long long然后它将是

传递给printf

(不确定如何 - 这个是基于MIPS的平台)和printf是一个可变的

函数。


我必须丢失一些东西或编译器坏了吗?
Consider this piece of code

unsigned long data = 124; /* Data is actually part of a big struct but
to simplify */
printf(" %-5llu ", (unsigned long long)(data*1000));
<memory address say 60000000>: 00000000 00000D77 00000000
00000000
0xD77 is 3447

Assume data is at the beginning of memory address 60000000 and this is
a big endian machine (MIPS based).
The compiler is doing two load word from 60000000 and 60000000+4 and
then multiplying it by 1000 and then prints 3447000 which is not
expected.

My understanding is that the 1000 will default to int
Then it will be promoted to unsigned long (since on our platform long
and int are of same size)
then operation will be carried out in unsigned long (result I mean).
then *result* will be cast to unsigned long long and then it will be
passed to printf
(not sure how - this is MIPS based platform) and printf is a variadic
function.

I must be missing something or the compiler is broken?



在后续文章中你写道:

| unsigned long data = 0x124

|

|请忽略上一篇文章中的上述初始化行。

|我只想说:数据的类型是无符号长的。


但是在原始帖子中,数据初始化为124,而不是0x124。

事实上你错误地引用了自己的帖子并没有让我充满信心,你已经向我们展示了你的实际代码和数据。您显示格式为% - 5llu的

printf调用,但您的输出是带有前导零的
十六进制。


我不知道你发布了什么问题。发布一个显示问题的小型完整程序,以及

实际输出以及您对输出的预期。不要重新输入

程序或输出;完全复制并粘贴它。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *< http:// users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。

In a followup you wrote:
| unsigned long data = 0x124
|
| please ignore the above initialization line in previous post.
| I only wanted to say : the type of data is unsigned long.

But in your original post, data was initialized to 124, not 0x124.
The fact that you misquoted your own post doesn''t fill me with
confidence that you''ve shown us your actual code and data. You show a
printf call with a format of "%-5llu", but your output is in
hexadecimal with leading zeros.

I can''t tell from what you posted what the problem might be. Post a
small complete program that exhibits the problem, along with the
actual output and what you expected the output to be. Don''t re-type
either the program or the output; copy-and-paste it exactly.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


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

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