减去功能疯狂

时间:2014-12-23 01:22:22

标签: c visual-studio-2012

这里只是一个程序,可以从另一个数字中减去一个数字。我得到-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;
}

1 个答案:

答案 0 :(得分:1)

对于double a %lf应该使用而不是%f,因为在scanf_s / scanf中,当你使用%f传递double时,它将被指示为4字节实体,而double是8个字节(在Visual Studio中)。

编译器实现的大小可能不同,但大多数上述原因保持相关。