簡體   English   中英

C 語言沒有給我小數形式的 output

[英]C language not giving me the output in decimals

//Converts Farenheit tempretaure to into the celsius scale

#include <stdio.h>
#define FREEZING_PT 32.0f
#define FACTOR 5.0f/9.0f

int main(void)
{
    float faren,c;

    printf("Enter the Farenheit temperature: ");
    scanf("%f",&faren);
    float c = (faren - FREEZING_PT)*FACTOR;

    printf("The required celsius tempreature is: %.1f\n", c);

    return 0;
}

我是一個完整的 C 初學者,這可能是非常初級的,但我無法弄清楚這里的問題。

在上面的代碼中,我返回的值始終是 integer 值中的攝氏溫度,即使它是 float 類型。 例如,如果華氏溫度為 0°,則攝氏溫度的結果應為 -17.7°,但我得到的結果僅為 -17°。

編輯代碼:

//Converts Farenheit tempretaure to into the celsius scale

#include <stdio.h>
#define FREEZING_PT 32.0f
#define FACTOR 5.0f/9.0f

int main(void)
{
    float faren,c;

    printf("Enter the Farenheit temperature: ");
    scanf("%f",&faren);
    c = (faren - FREEZING_PT)*FACTOR;

    printf("The required celsius tempreature is: %.1f\n", c);

    return 0;
}

在您的代碼中,您已兩次聲明變量“c”。 刪除變量“c”的第一個聲明后,它可以正常工作。

我得到正確的 output。

輸入華氏溫度:0

所需攝氏溫度為:-17.8

輸入華氏溫度:1

所需攝氏溫度為:-17.2

輸入華氏溫度:2

所需攝氏溫度為:-16.7

刪除變量“c”的第一個聲明。 它應該工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM