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型吗
。
分类:
c/c++
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2022-08-19 c语言中利用函数递归求阶乘
2022-08-19 c语言中枚举类型