相关疑难解决方法(0)

正确的printf格式说明符为double

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)

c floating-point double printf format-specifiers

451
推荐指数
5
解决办法
96万
查看次数

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

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 …

c double

2
推荐指数
1
解决办法
89
查看次数

C print -nan中的sqrt函数

在做一些代码练习时,我观察到由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)

为什么会发生这种情况.

c sqrt

1
推荐指数
1
解决办法
702
查看次数

标签 统计

c ×3

double ×2

floating-point ×1

format-specifiers ×1

printf ×1

sqrt ×1