doubleprintf中正确的格式说明符是什么?是它%f还是它%lf?我相信它%f,但我不确定.
#include <stdio.h>
int main()
{
double d = 1.4;
printf("%lf", d); // Is this wrong?
}
Run Code Online (Sandbox Code Playgroud) 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 …
在做一些代码练习时,我观察到由sqrt函数引起的异常输出,
代码是,
#include<stdio.h>
#include<math.h>
int main()
{
double l,b,min_r,max_r;
int i;
scanf("%lf %lf",&b,&l);
printf("%lf %lf\n",sqrt(l*l+b*b),sqrt(b*b-l*l));
return(0);
}
Run Code Online (Sandbox Code Playgroud)
输出:
4 5
6.403124 -nan
Run Code Online (Sandbox Code Playgroud)
为什么会发生这种情况.