c变量似乎是打印地址而不是变量值
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
double f;
printf ("What is the temperature in Fahrenheit?\n");
scanf ("%d", &f);
double x = (f-32)*(5/9);
printf ("%d degrees Fahrenheit \n",f);
printf ("%d degrees Celsius",x);
system("PAUSE");
return 0;
}
f似乎是变量f的打印地址而不是值,它可能是一个语法错误。c变量似乎是打印地址而不是变量值
%d应为%F:
printf ("%f degrees Farenheit \n",f);
printf ("%f degress Celsius",x);
编辑:乔纳森指出的,你需要改变:
double x = (f-32)*(5/9);
到
double x = (f-32)*(5.0/9);
在C,整数除法(分红和除数都是整数类型的分部)截断,所以5/9总是0.
添加到其他人所说,%d
代表“十进制”(而不是%x
是“十六进制”)
浮点数,或在您的情况下双精度浮点数,使用%f
%d
表示“十进制整数”。您正在使用double
s,而不是int
s。因此在printf()
的scanf()
和%f
中使用%lf
。
尝试......
#include <stdio.h>
//#include <stdlib.h>
int main (void)
{
float c;
float f;
printf ("What is the temperature in Fahrenheit?\n");
scanf ("%f", &f);
c = (f-32)*(5.0/9.0);
printf ("%f degrees Fahrenheit \n",f);
printf ("%f degrees Celsius \n",c);
system("PAUSE");
return 0;
}
如果实际包含
9次了10年,当您使用printf
显示调试数据,发现其显示效果的bizzare,你有你的电话printf
错误。
这就是为什么我现在试图避免printf
。动态格式位是整齐的,但是调用的极端类型 - 不安全性使其经常成为错误的源头,而不是显而易见的。
给自己一个编译器,告诉你什么时候犯错误 - 并且学习规则,这样你就不会犯错误。 – 2010-02-03 16:29:55
你的第一个问题是double x = (f-32)*(5/9)
。 5/9
为零,因为您正在使用整数除法,所以x
对于所有输入将具有值0.0
。对于浮点除法,使用浮点数(如5.0/9.0
)。
二,%d
用于打印整数。使用%f
来打印浮点值。
请参阅* http://*.com/questions/210590/why-does-scanf-need-lf-for-doubles-when-printf-is-okay-with-just-f*关于使用* * scanf()**和** printf()**双打 – jschmier 2010-02-03 16:15:18
5/9 == 0; 5.0/9.0是你想使用的。 – 2010-02-03 16:26:38