我最后一直收到0作为答案。请帮忙
#include <stdio.h>
int Fahrenheit = 0;
double Celsius = 0.0;
double main(void)
{
printf("This program will convert the temperature from fahrenheit to celsius.\n");
printf("Please type in the temperature in fahrenheit followed by the enter key.\n");
scanf("%d%",&Fahrenheit);
Celsius = (5/9) * (Fahrenheit-32) ;
printf("Your temperature in celsius is %3.2f.\n", Celsius);
return(0);
}
答案 0 :(得分:1)
由于整数除法,请将5 / 9
更改为5.0 / 9.0
。另外,请Fahrenheit
double
并将scanf()
更改为
if (scanf("%lf", &Fahrenheit) == 1)
{
Celcius = 5.0 * (Fahrenheit - 32.0) / 9.0;
printf("Your temperature in celsius is %3.2f.\n", Celsius);
}
此外:
我见过许多奇特的main()
签名,现在
double main(void);
忽略scanf()
的返回值会导致潜在的未定义行为。如果我有一位老师禁止if
陈述但需要scanf()
我会退出并找到一位优秀的导师。
但当然,如果我在学习,我怎么知道忽略scanf()
的回报值是不是很糟糕?这是令人悲伤的部分,许多人甚至不知道它返回一个值或者它失败了,例如试试这个
int value;
if (scanf("%d", &value) != 1)
fprintf(stderr, "Error, invalid input\n");
else
fprintf(stdout, "Ok, so your input is `%d'\n", value);
键入"abcd"
而非数字,会发生什么?