2016-01-23 106 views
0

我这里有这样的代码:(在Mac上使用克利翁)不调用地址,但持股量恢复0.000-

#include <stdio.h> 

float convFtoC(int *, float *); 

int main() 
{ 
int min, increment, fahrenheit; 
float celsius; 

increment = 5; 
min = 0; 


printf("Please enter a max temperature in Fahrenheit: \n"); 
scanf("%d", &fahrenheit); 

convertFtoC(&fahrenheit, &celsius); 

printf("%d degrees Fahrenheit converted is %.4f degrees Celsius \n", fahrenheit, celsius); 

printf("%d is the lowest F. The temperatures increment by %d.", min, increment); 

return 0; 
} 

float convFtoC(int *pfahrenheit, float *pcelsius) 
{ 
*pcelsius = (*pfahrenheit - 32) * (5/9); 

return 0; 
} 

我为摄氏输出变为0.00,所有输入端,我想不通为什么。我不打电话给摄氏度地址,所以我很困惑,为什么会发生这种情况。以下是输出示例:

Please enter a max temperature in Fahrenheit: 
60 
60 degrees Fahrenheit converted is 0.00 degrees Celsius 
0 is the lowest F. The temperatures increment by 5. 
Process finished with exit code 0 
+3

'5/9'有一个错字错 - >'5.0F/9' – BLUEPIXY

+0

哇这样的作品,非常感谢。有没有办法让它在不篡改数字的情况下以这种方式工作? – Xirol

+1

或'((float)5/9);'。问题是“(5/9)”执行整数除法,因为它的两个操作数都是“int”类型,所以它的计算结果为零。 –

回答

4

您必须强制运算在浮点模式下运行。

这可以通过改变来完成55.099.0

*pcelsius = (*pfahrenheit - 32) * (5.0/9); 

虽然没有必要convFtoC返回类型为float;它可以是void

而且,你在呼唤convFtoC