2014-12-23 40 views
1

只是一個程序在這裏從另一個數字中減去一個數字。我得到-23882489428948248829 ...等差異...你能說出原因嗎?減去功能瘋狂

#include <stdio.h> 

double minus(double a, double b) { // set up minus function 
    double difference = a - b; 
    return difference; 
} 

int main() //begin program 
{ 
    double a; //declare variables in this scope 
    double b; 

    printf("Enter the first number:\n"); 
    scanf_s("%f", &a); //get a from user 

    printf("Enter the second number:\n"); 
    scanf_s("%f", &b); //get b from user 

    printf("The difference is %f\n", minus(a,b)); //print results 
    return 0; 
} 
+7

在'scanf'中使用''%lf「''雙'' – BLUEPIXY

+0

@BLUEPIXY但是我記得Turbo C的舊日,'%f'對於float和double都可以嗎?但在Visual Studio中,它表現得很奇怪。 –

+0

爲什麼人們沒有評論就冷靜下來? –

回答

1

對於double a%lf當你%F雙通應該被用來代替%f因爲在scanf_s/scanf的,這將被指示在該4字節的實體,和雙爲8個字節(在Visual Studio)。

編譯器實現的大小可能不同,但大多數上述原因保持相關。

+2

尺寸不一定是4和8 –

+0

@MattMcNabb我知道int的大小與每個操作系統不同,但是也是double和float? –

+1

所有類型都可以有任意大小(字符類型爲'1')。 –