Float, Double data types confusion in external functions (C)

Xut*_*tuh 2 c double

The code below compiles alright:

#include <stdio.h>

double add(double summand1, double summand2)
{
    double sum;
    sum = summand1+summand2;
    return sum;
}

double subtract(double minuend, double subtrahend)
{
    double diff;
    diff = minuend-subtrahend;
    return diff;
}

int main()
{
    double a,b,c,d;
    printf("Enter Operand 1:\n");
    scanf("%d",&a);
    printf("Enter Operand 2:\n");
    scanf("%d",&b);
    // Add
    c = add(a,b);

    // Subtract
    d = subtract(a,b);

    printf("Sum: %.3f\n", c);
    printf("Difference: %.3f\n", d);

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

However, when entering 5 and 5 the result is 0.000 (wrong) and 0.000 (expected). When entering a decimal number (e.g. 2.5), the second prompt is skipped altogether and two random numbers are printed for sum and difference. The problem must be with the data types double and float, which I seem to be using incorrectly.

Ala*_*lan 5

在您中,scanf()您使用了%d用于整数的格式说明符。当您将整个变量声明为double时,请改用%lf(浮点数)。我尝试了该代码,并使用正确的说明符,该代码有效。