的printf(QUOT;%d",浮动) [英] printf("%d",float)
问题描述
大家好,
我刚刚遇到以下程序:
#include< stdio.h>
int main()
{
浮动a = 12.5;
printf("%d \ n",a);
printf("%d \ n",*(int *)& a);
返回0;
}
该程序打印0和1095237362.但是,在我看来它应该在两个地方打印12美元b $ b。谁能告诉我哪里错了?
Hi all,
I just came across the following program:
#include <stdio.h>
int main()
{
float a = 12.5;
printf("%d\n", a);
printf("%d\n", *(int *)&a);
return 0;
}
The program prints 0 and 1095237362. However, in my opinion it should
print 12 at both the places. Can anybody tell me where I am wrong?
推荐答案
ca ******** @ yahoo.com 说:
大家好,
我刚刚遇到以下程序:
#include< stdio.h>
int main()
{
浮动a = 12.5;
printf("%d \ n",a);
printf("%d \ n",*(int *)& a);
返回0;
}
程序打印0和1095237362.但是,在我看来,这两个地方应该打印12美元。谁能告诉我哪里错了?
Hi all,
I just came across the following program:
#include <stdio.h>
int main()
{
float a = 12.5;
printf("%d\n", a);
printf("%d\n", *(int *)&a);
return 0;
}
The program prints 0 and 1095237362. However, in my opinion it should
print 12 at both the places. Can anybody tell me where I am wrong?
你认为%d是
浮点数的合适格式说明符你错了,你认为你错了有意义地将浮动的
地址转换为int的地址。
-
Richard Heathfield< http: //www.cpax.org.uk>
电子邮件:-http:// www。 + rjh @
谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>
Usenet是一个奇怪的放置" - dmr 1999年7月29日
You are wrong in thinking that %d is the appropriate format specifier for a
float, and you are wrong in thinking that you can meaningfully cast the
address of a float into the address of an int.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
ca ******* *@yahoo.com 写道:
ca********@yahoo.com wrote:
大家好,
我刚刚遇到以下程序:
#include< stdio.h>
int main()
{
浮动a = 12.5;
printf("%d \ n",a);
Hi all,
I just came across the following program:
#include <stdio.h>
int main()
{
float a = 12.5;
printf("%d\n", a);
printf期望并将相应的参数视为有符号的int,
,而你已经传递了一个浮点值。
printf expects and treats the corresponding argument as a signed int,
while you''ve passed it a float value.
printf("%d \ n",*(int *)& a);
printf("%d\n", *(int *)&a);
与上述相同,还有一个问题:
不同类型的指针值不需要在C中兼容,除了
将任何指针转换为void *并再次返回将是
好吧。
Same as above and an additional problem:
Pointers values of different types need not be compatible in C, except
that a conversion of any pointer to void * and back again shall be
okay.
返回0;
}
该程序打印0和1095237362.但是,在我看来它应该在两个地方打印12美元b $ b。谁能告诉我哪里错了?
return 0;
}
The program prints 0 and 1095237362. However, in my opinion it should
print 12 at both the places. Can anybody tell me where I am wrong?
是的。你写了一个不正确的程序,所以错误的结果是
预计。
阅读:
< http://www.c-faq.com/>
Yes. You''ve written an incorrect program, so incorrect results are to be
expected.
Read:
<http://www.c-faq.com/>
10月30日晚上8:48,candy_i ... @ yahoo.com写道:
On Oct 30, 8:48 pm, candy_i...@yahoo.com wrote:
大家好,
我刚刚遇到以下程序:
#include< stdio.h>
int main()
{
浮动a = 12.5;
printf("%d \ n",a);
printf("%d \ n",*(int *)& a);
返回0;
}
程序打印0和1095237362.但是,在我看来它应该打印
打印这两个地方都有12个。谁能告诉我哪里错了?
Hi all,
I just came across the following program:
#include <stdio.h>
int main()
{
float a = 12.5;
printf("%d\n", a);
printf("%d\n", *(int *)&a);
return 0;
}
The program prints 0 and 1095237362. However, in my opinion it should
print 12 at both the places. Can anybody tell me where I am wrong?
指针转换不会将一种类型的值转换为另一种类型。你的
代码有点像这样:
struct {
char a;
} A;
struct {
浮动b;
} B;
printf("%c",((* * )& B) - > a);
或
printf("%f",((B *)& A) - > a) ;
后者会出现段错误/崩溃。
如果sizeof(int)!= sizeof(float)你的代码会有*非常可能
坠毁了。这就是为什么你应该*非常小心
指针的原因之一,并且在使用指针时从不做任何花哨的事情
除非你完全知道它做了什么,为什么它是这样,而且任何
副作用。要非常小心,*特别是*当你需要传递
void *参数和struct-cast指针来获取数据时。
回到不同的数字,
浮点数= [符号位] [7指数位] [23尾数位]
12.5 = 0 10000010 10010000000000000000000
= 0x41480000
= 1095237632
这就是你的号码所在的地方。
来源: http://en.wikipedia.org/wiki/Single_precision
Pointer casting does not convert values of one type to another. Your
code is sort of like this:
struct {
char a;
} A ;
struct {
float b;
} B;
printf("%c", ((A*)&B)->a);
OR
printf("%f", ((B*)&A)->a);
The latter will segfault/crash.
If sizeof(int) != sizeof(float) your code would have *very* likely
crashed. This is one of the reasons why you should be *very* careful
with pointers and never do anything fancy when working with pointers
UNLESS YOU COMPLETELY KNOW WHAT IT DOES, WHY IT DOES THAT, AND ANY
SIDE-EFFECTS. Be very careful, *especially* when you need to pass
void* arguments and struct-cast the pointer to get the data.
Getting back to the different number,
float = [sign bit] [7 exponent bits] [23 mantissa bits]
12.5 = 0 10000010 10010000000000000000000
= 0x41480000
= 1095237632
That''s where your number came from.
Source: http://en.wikipedia.org/wiki/Single_precision
>
这篇关于的printf(QUOT;%d",浮动)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!