鱼C论坛

 找回密码
 立即注册
查看: 3819|回复: 28

[已解决]double,longdouble除了范围的区别

[复制链接]
发表于 2021-8-27 11:30:53 | 显示全部楼层 |阅读模式
5鱼币
同样的程序,为什么double执行正确,而longdouble执行错误。求大佬解释
最佳答案
2021-8-27 11:30:54
  1. long double 和 double 的输出格式是不一样的
  2. 在我这边是 Lf 和 lf

  3. #include<stdio.h>

  4. int main()
  5. {
  6.     int r;
  7.     double c,s;
  8.     r=5;
  9.     c=2*3.14*r;
  10.     s=3.14*r*r;
  11.     printf("c=%6.2lf,s=%6.2lf",c,s);

  12.     return 0;
  13. }

  14. #include<stdio.h>

  15. int main()
  16. {
  17.     int r;
  18.     long double c,s;
  19.     r=5;
  20.     c=2*3.14*r;
  21.     s=3.14*r*r;
  22.     printf("c=%6.2Lf,s=%6.2Lf",c,s);

  23.     return 0;
  24. }
复制代码
捕获1234.PNG
捕获12.PNG
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-8-27 11:30:54 | 显示全部楼层    本楼为最佳答案   
  1. long double 和 double 的输出格式是不一样的
  2. 在我这边是 Lf 和 lf

  3. #include<stdio.h>

  4. int main()
  5. {
  6.     int r;
  7.     double c,s;
  8.     r=5;
  9.     c=2*3.14*r;
  10.     s=3.14*r*r;
  11.     printf("c=%6.2lf,s=%6.2lf",c,s);

  12.     return 0;
  13. }

  14. #include<stdio.h>

  15. int main()
  16. {
  17.     int r;
  18.     long double c,s;
  19.     r=5;
  20.     c=2*3.14*r;
  21.     s=3.14*r*r;
  22.     printf("c=%6.2Lf,s=%6.2Lf",c,s);

  23.     return 0;
  24. }
复制代码

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +3 收起 理由
wyz20010429 + 5 + 5 + 3 鱼C有你更精彩^_^

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-8-27 11:37:01 | 显示全部楼层
发代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-8-27 11:47:57 | 显示全部楼层

#include<stdio.h>

int main()
{
        int r;
        long double c,s;
        r=5;
        c=2*3.14*r;
        s=3.14*r*r;
        printf("c=%6.2Lf,s=%6.2Lf",c,s);
       
        return 0;
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-8-27 11:48:28 | 显示全部楼层

这个是longdouble的那个
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-8-27 11:49:30 | 显示全部楼层
文件开头加个这个试试
  1. #define _USE_MINGW_ANSI_STDIO 1
复制代码

评分

参与人数 1荣誉 +4 鱼币 +4 贡献 +2 收起 理由
wyz20010429 + 4 + 4 + 2 鱼C有你更精彩^_^

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-8-27 12:11:19 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-8-27 12:11:53 | 显示全部楼层
逃兵 发表于 2021-8-27 11:49
文件开头加个这个试试

谢谢,兵哥
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-8-27 12:18:58 | 显示全部楼层

但是大佬你运行下你发的下面那个代码,我试了下结果依据是0,
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-8-27 12:23:38 | 显示全部楼层
wyz20010429 发表于 2021-8-27 12:18
但是大佬你运行下你发的下面那个代码,我试了下结果依据是0,

在我这边 long double 没问题,但是 double 什么都不显示
  1. $ cat main.c
  2. #include<stdio.h>

  3. int main()
  4. {
  5.     int r;
  6.     long double c,s;
  7.     r=5;
  8.     c=2*3.14*r;
  9.     s=3.14*r*r;
  10.     printf("c=%6.2Lf,s=%6.2Lf",c,s);

  11.     return 0;
  12. }
  13. $ gcc -g -Wall -o main main.c
  14. $ ./main
  15. c= 31.40,s= 78.50$ vim main.c
  16. $ cat main.c
  17. #include<stdio.h>

  18. int main()
  19. {
  20.     int r;
  21.     double c,s;
  22.     r=5;
  23.     c=2*3.14*r;
  24.     s=3.14*r*r;
  25.     printf("c=%6.2Lf,s=%6.2Lf",c,s);

  26.     return 0;
  27. }
  28. $ gcc -g -Wall -o main main.c
  29. main.c: In function ‘main’:
  30. main.c:10:20: warning: format ‘%Lf’ expects argument of type ‘long double’, but argument 2 has type ‘double’ [-Wformat=]
  31.    10 |     printf("c=%6.2Lf,s=%6.2Lf",c,s);
  32.       |               ~~~~~^           ~
  33.       |                    |           |
  34.       |                    long double double
  35.       |               %6.2f
  36. main.c:10:29: warning: format ‘%Lf’ expects argument of type ‘long double’, but argument 3 has type ‘double’ [-Wformat=]
  37.    10 |     printf("c=%6.2Lf,s=%6.2Lf",c,s);
  38.       |                        ~~~~~^    ~
  39.       |                             |    |
  40.       |                             |    double
  41.       |                             long double
  42.       |                        %6.2f
  43. $ ./main
  44. $
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-8-27 12:31:42 | 显示全部楼层
人造人 发表于 2021-8-27 12:23
在我这边 long double 没问题,但是 double 什么都不显示

啊,估计是我编译器的问题了,我的是DeV-C++可能这里有点bug
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-8-27 12:33:02 | 显示全部楼层
wyz20010429 发表于 2021-8-27 12:31
啊,估计是我编译器的问题了,我的是DeV-C++可能这里有点bug

不是编译器的问题,是你用的输出格式不对,不知道在你那边的输出格式应该是什么
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-8-27 12:34:56 | 显示全部楼层
人造人 发表于 2021-8-27 12:33
不是编译器的问题,是你用的输出格式不对,不知道在你那边的输出格式应该是什么

我的无论是Lf还是lf,只要是longdouble就运行为0
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-8-27 12:35:36 | 显示全部楼层
人造人 发表于 2021-8-27 12:33
不是编译器的问题,是你用的输出格式不对,不知道在你那边的输出格式应该是什么

用double的话,lf还是Lf都运行正确结果
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-8-27 12:44:55 | 显示全部楼层
wyz20010429 发表于 2021-8-27 12:34
我的无论是Lf还是lf,只要是longdouble就运行为0

那就不知道了,不知道你那边的 long double 是什么格式
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-8-27 12:54:14 | 显示全部楼层
人造人 发表于 2021-8-27 12:44
那就不知道了,不知道你那边的 long double 是什么格式

Lf.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-8-27 13:18:45 | 显示全部楼层

用命令行编译,看看 gcc 有什么提示
  1. gcc -g -Wall -o main main.c
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-8-27 13:41:14 | 显示全部楼层
人造人 发表于 2021-8-27 13:18
用命令行编译,看看 gcc 有什么提示
  1. demo.c: In function 'main':
  2. demo.c:10:12: warning: unknown conversion type character 'L' in format [-Wformat=]
  3.      printf("c=%6.2Lf,s=%6.2Lf",c,s);
  4.             ^
  5. demo.c:10:12: warning: unknown conversion type character 'L' in format [-Wformat=]
  6. demo.c:10:12: warning: too many arguments for format [-Wformat-extra-args]
  7. demo.c:10:12: warning: unknown conversion type character 'L' in format [-Wformat=]
  8. demo.c:10:12: warning: unknown conversion type character 'L' in format [-Wformat=]
  9. demo.c:10:12: warning: too many arguments for format [-Wformat-extra-args]
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-8-27 14:03:24 | 显示全部楼层

把 Lf 改成 f 然后
gcc -g -Wall -o main main.c
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-8-27 14:22:23 | 显示全部楼层
人造人 发表于 2021-8-27 14:03
把 Lf 改成 f 然后
gcc -g -Wall -o main main.c
  1. demo.c: In function 'main':
  2. demo.c:11:12: warning: format '%f' expects argument of type 'double', but argument 2 has type 'long double' [-Wformat=]
  3.      printf("c=%6.2f,s=%6.2f\n",c,s);
  4.             ^
  5. demo.c:11:12: warning: format '%f' expects argument of type 'double', but argument 3 has type 'long double' [-Wformat=]
  6. demo.c:11:12: warning: format '%f' expects argument of type 'double', but argument 2 has type 'long double' [-Wformat=]
  7. demo.c:11:12: warning: format '%f' expects argument of type 'double', but argument 3 has type 'long double' [-Wformat=]
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-26 20:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表