浮动到int型铸造? [英] Float to int type casting?

查看:87
本文介绍了浮动到int型铸造?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




1 int main()

2 {

3浮动a = 17.5;

4 printf("%d \ n",a);

5 printf("%d \ n",*(int *)& a);

6返回0;

7}


输出结果为:


0

1099694080

我无法理解为什么第一个printf打印0和另一个

,1099694080 ...


我认为与简单的类型转换有所不同。这是一个

未定义的行为吗?

请让我理解。


问候。

Hi,

1 int main()
2 {
3 float a = 17.5;
4 printf("%d\n", a);
5 printf("%d\n", *(int *)&a);
6 return 0;
7 }

The output is:

0
1099694080

I could not understand why the first printf prints 0 and the other
,1099694080...

I think there is sth different from simple type casting.Is it an
undefined behaviour?
Please let me understand.

Regards.

推荐答案

Enekajmer写道:
Enekajmer wrote:


1 int main()
2 {
3浮动a = 17.5;
4 printf("%d \ n",a);


这里你试图用转换说明符打印浮点值

for int。结果未定义。

5 printf("%d \ n",*(int *)& a);


在这里,你重新解释了浮点数的表示。结果

是实现定义的。

6返回0;
7}

输出结果为:

0
1099694080

我无法理解为什么第一个printf打印0和另一个
,1099694080 ......

我认为有与简单类型铸造不同。这是一种不确定的行为吗?
Hi,

1 int main()
2 {
3 float a = 17.5;
4 printf("%d\n", a);
Here you''re trying to print a float value with a conversion specifier
for int. The results are undefined.
5 printf("%d\n", *(int *)&a);
Here, you''re reinterpreting the representation of the float. The results
are implementation defined.
6 return 0;
7 }

The output is:

0
1099694080

I could not understand why the first printf prints 0 and the other
,1099694080...

I think there is sth different from simple type casting.Is it an
undefined behaviour?




[这个词是'某事'']


见上文。


HTH,

- g

-

Artie Gold - 德克萨斯州奥斯汀
http://goldsays.blogspot.com
http://www.cafepress.com/goldsays
如果你没有什么可隐瞒的,你就不会尝试!



[the word is `something'']

See above.

HTH,
--ag
--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
http://www.cafepress.com/goldsays
"If you have nothing to hide, you''re not trying!"




Artie Gold写道:

Artie Gold wrote:
Enekajmer写道:
Enekajmer wrote:


1 int main()
2 {
3浮动a = 17.5;
4 printf("%d \ n",a);
Hi,

1 int main()
2 {
3 float a = 17.5;
4 printf("%d\n", a);


在这里,您尝试使用转换说明符
为int打印浮点值。结果未定义。



Here you''re trying to print a float value with a conversion specifier
for int. The results are undefined.

5 printf("%d \ n",*(int *)& a);
5 printf("%d\n", *(int *)&a);


在这里,你重新解释浮动的表示。结果
是实现定义的。



Here, you''re reinterpreting the representation of the float. The results
are implementation defined.

6返回0;
7}

输出结果为:

0
1099694080

我无法理解为什么第一个printf打印0和另一个
,1099694080 ......

我认为有......与简单类型铸造不同。这是一种未定义的行为吗?
6 return 0;
7 }

The output is:

0
1099694080

I could not understand why the first printf prints 0 and the other
,1099694080...

I think there is sth different from simple type casting.Is it an
undefined behaviour?



[这个词是'某事'']

见上文。

HTH,
- g
-
Artie Gold - 德克萨斯州奥斯汀
http://goldsays.blogspot.com
http://www.cafepress.com/goldsays
如果你没有什么可隐瞒的,你就不会尝试!



[the word is `something'']

See above.

HTH,
--ag
--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
http://www.cafepress.com/goldsays
"If you have nothing to hide, you''re not trying!"




谢谢我了解了行为的类型,但我想了解更多细节的原因。举个例子,这个片段没有问题

以下代码:

....

浮动a = 17.5;

int b = a; (从float到int的类型转换)

printf(" b - >%d",b); //通常情况下,打印b - > 17

....


根据这个例子,我猜到了输出我编译之前的代码也是17

。相比之下,它的输出为0 ...


printf(a - >%d,a)// a是浮点型

那么,这种情况的原因是什么?IEEE-754?

问候。



Thanks i learnt the types of behaviours but i want to learn causes in
more details.To give an example, there is no problem for the snippet
code below:
....
float a=17.5;
int b=a; (type casting from float to int)
printf("b-->%d",b); // as normally, this prints "b-->17"
....

according to this example i guessed the output of the code below as 17
too before i compiled. As contrast its output is 0...

printf("a-->%d",a) //a is in float type
So, what are the causes of this case?The IEEE-754?

Regards.




" Artie Gold" < AR ******* @ austin.rr.com>在消息中写道

news:44 ************ @ individual.net ...

"Artie Gold" <ar*******@austin.rr.com> wrote in message
news:44************@individual.net...
Enekajmer写道:
Enekajmer wrote:


1 int main()
2 {
3浮动a = 17.5;
4 printf("%d \ n,a );
Hi,

1 int main()
2 {
3 float a = 17.5;
4 printf("%d\n", a);



在这里,您尝试使用转换说明符
为int打印浮点值。结果是未定义的。



Here you''re trying to print a float value with a conversion specifier
for int. The results are undefined.




实际上,你正在尝试打印一个double值,因为当它''时,浮动是

。不受原型中正式参数的约束。


如果不是这样的话,你可能会得到两个相同的输出

printfs。


-

RSH



Actually, you''re trying to print a double value, because the float is
promoted when it''s not governed by a formal parameter in a prototype.

If it weren''t for that, you''d probably get the same output from both
printfs.

--
RSH


这篇关于浮动到int型铸造?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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