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;
}
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.