编译C代码时发生错误

时间:2012-09-28 10:54:15

标签: c

通过键盘输入华氏度的城市温度。现在我需要编写一个程序将此温度转换为摄氏度。

所以这是公式:

°C = (°F -  32)  x  5/9

示例输入/输出:

Enter Temperature of Dhaka in Fahreinheit: 98.6
Temperature of Dhaka in Centigrade 37.0 C
Now, i have tried with this, but not works.

代码:

# include <stdio.h>

void main()
{
    float C;
    printf("Pleas Enter Your Fahreinheit Value to see in centrigate=");
    scanf("%d",&C);

    printf(C);

    float output;
    output=(C-32)*(5/9);

    printf("The centrigate Value is = %.2lf\n\n" ,output);
}

谁能告诉我出了什么问题?

5 个答案:

答案 0 :(得分:8)

void main()
{
  float far;
  printf("Pleas Enter Your Fahreinheit Value to see in centrigate=");
  scanf("%f",&far);

 // printf(C);

 float cel;
 cel =(far-32)*(5.0/9.0);

 printf("The centrigate Value is = %.2lf\n\n" ,cel);
}
  1. 5/9是整数除法,它为您提供0。你需要漂浮。所以5.0/9.0得到小数部分。
  2. 我不知道你为什么这么做printf(C);。那简直不行。使用

     printf("c = %f",c);  
    
  3. float的格式说明符是%f%d用于整数。

  4. 您提供C来存储farenheit。现在,这没有错。但可能后来引起混乱。尝试在代码中使用有意义的名称,以使其可读。名字越久越好。

答案 1 :(得分:3)

问题:

  • scanf()中的格式说明符应为%f,而不是%d的{​​{1}}:

    int
  • printf()的第一个参数应该是/* scanf() returns number of assignments made. Check it to ensure a float was successfully read. */ if (1 == scanf("%f", &C)) { } ,而不是const char*

    float

答案 2 :(得分:1)

您应该进行一些更改,请参阅代码注释:

# include <stdio.h>

void main()
{
    float C;
    float output; //Better to declare at the beginning of the block

    printf("Pleas Enter Your Fahreinheit Value to see in centrigate=\n");
    scanf("%f",&C);    //Scanf need %f to read float

    printf("%f\n", C); //becareful with the printf, they need format too.

    output=(C-32)*(5.0/9);    //if you put 5/9 is not a float division, and returns int.
                             //you should add 5.0/9.

    printf("The centrigate Value is = %.2lf\n\n" ,output);
}

我认为这就是全部。

答案 3 :(得分:0)

printf(C);行应为printf("%f\n", C);

答案 4 :(得分:0)

printf("%f\n",C)代替printf(C)float output应位于代码的开头,例如float C

相关问题