首页 > 解决方案 > 我正在尝试使用 c 中的函数编写华氏到摄氏度的代码

问题描述

我正在尝试使用一个函数将华氏温度转换为摄氏温度。我想知道我在哪里犯了错误。

#include <stdio.h>

void degrees_Fto_C(float f){
    float c;
    c=(5/9)*(f–(32));
    printf("%f",c);
}

int main(){
    float temp;
    printf("Enter the temperature in Fahrenheit: \n");
    scanf("%f",&temp);
    degrees_Fto_C(temp);
    return 0;
}

错误信息是:

C:\Users\Pranavs\Desktop\Untitled1.cpp  In function 'void degrees_Fto_C(float)':
C:\Users\Pranavs\Desktop\Untitled1.cpp  [Error] 'f' cannot be used as a function

标签: c

解决方案


第 4 行中 f 后面的字符有误。c=(5/9)*(f–(32))需要是c=(5.0/9) * (f-(32)). 你的减号是一个unicode字符,你需要ASCII。如果你退格它并用正常的减号替换它,它将编译。

此外,你正在做整数运算,你总是会得到零。如果在 5 或 9 之后添加小数点,效果会更好。

这是您的程序的工作版本:

#include <stdio.h>
void degrees_Fto_C(float f) {
    float c;
    c = (5.0 / 9) * (f - (32));
    printf("%f", c);
}
int main() {
    float temp;
    printf("Enter the temperature in Fahrenheit: \n");
    scanf("%f", &temp);
    degrees_Fto_C(temp);
    return 0;
}

推荐阅读