乘/加双倍返回错误

0 c

我编写了这个程序来将摄氏度转换为华氏度,但该程序只返回不正确的数字。由于我对 C 语言或编程很陌生,我不知道应该尝试解决什么问题。

#include <stdio.h>
#include <stdlib.h>

int main(void){

        double a;
        double b = a * 1.8;
        double c = b + 32;

        printf("Enter a temp in celcius");
        scanf("%lf", &a);
        printf("%f", &c);



        return 0;
}
Run Code Online (Sandbox Code Playgroud)

Jos*_*ica 5

阅读您的编译器给您的警告:

q59587632.c:12:22: warning: format specifies type 'double' but the argument has
      type 'double *' [-Wformat]
        printf("%f", &c);
                ~~   ^~
q59587632.c:7:20: warning: variable 'a' is uninitialized when used here
      [-Wuninitialized]
        double b = a * 1.8;
                   ^
q59587632.c:6:17: note: initialize the variable 'a' to silence this warning
        double a;
                ^
                 = 0.0
2 warnings generated.
Run Code Online (Sandbox Code Playgroud)

第一个警告是因为您正在打印c内存中的位置而不是它的值。第二个警告是因为您在ascanf. 这些警告并非多余;它们是您问题的确切原因。这是您的程序在修复了这两件事后的样子:

#include <stdio.h>
#include <stdlib.h>

int main(void){

        double a;

        printf("Enter a temp in celcius");
        scanf("%lf", &a);

        double b = a * 1.8;
        double c = b + 32;

        printf("%f", c);



        return 0;
}
Run Code Online (Sandbox Code Playgroud)