c语言中%f和%lf读入浮点型数据

 

001、a、%lf、和 %f读入double型值

[root@PC1 test]# ls
test.c
[root@PC1 test]# cat test.c              ## 测试程序
#include <stdio.h>

int main(void)
{
        double i, j;                    // 声明两个double型变量

        printf("i: "); scanf("%lf", &i);    // 利用%lf读入double型变量
        printf("j: "); scanf("%f", &j);     // 利用%f读入就double型变量

        printf("i = %lf\n", i);
        printf("j = %lf\n", j);

        return 0;
}
[root@PC1 test]# gcc test.c -o kkk
[root@PC1 test]# ls
kkk  test.c
[root@PC1 test]# ./kkk                       ## 结果表明 %f无法读入double型变量。
i: 3.14
j: 3.14
i = 3.140000
j = 0.000000

 。

 

002、%lf和%f读入float型变量

[root@PC1 test]# ls
test.c
[root@PC1 test]# cat test.c         ## 测试c程序
#include <stdio.h>

int main(void)
{
        float i,j;                   //声明两个float型变量

        printf("i: "); scanf("%lf", &i);    // %lf读入
        printf("j: "); scanf("%f", &j);     // %f读入

        printf("i = %lf\n", i);
        printf("j = %lf\n", j);

        return 0;
}
[root@PC1 test]# gcc test.c -o kkk
[root@PC1 test]# ls
kkk  test.c
[root@PC1 test]# ./kkk                   ## 结果表明 %lf无法正确读入float型变量
i: 3.14
j: 3.14
i = 126443839488.000000
j = 3.140000

 。

 

综合001和002表明:

%f无法正确读入double型变量;

%lf无法正确读入float型变量;

读入double型变量只能使用%lf;

读入float型变量只能使用%f;

 

%lf可以输出double型 和float型;

%f可以输出double型和float型吗

 

posted @ 2024-08-19 22:51  小鲨鱼2018  阅读(1)  评论(0编辑  收藏  举报