gpt4 book ai didi

c - 如何在C中打印long double的范围

转载 作者:行者123 更新时间:2023-11-30 20:21:34 45 4
gpt4 key购买 nike

在尝试用 C 打印 Long Double 的最大和最小范围时,我偶然发现了不同格式说明符的问题,因此我尝试了在以下代码中找到的所有建议:

#include<stdio.h>
#include<limits.h>
#include<float.h>
#include<math.h>
int main(void)
{
printf("Data Type Memory Size Range\n");
printf("------------------------------------------------------------- \n");
printf("long double %d bytes %Lf to %Lf |>(using %%Lf as format specifier)\n",sizeof(long double),LDBL_MIN,LDBL_MAX);
printf("long double %d bytes %lf to %lf |>(using %%lf as format specifier)\n",sizeof(long double),LDBL_MIN,LDBL_MAX);
printf("long double %d bytes %LG to %LG |> (using %%LG as format specifier)\n",sizeof(long double),LDBL_MIN,LDBL_MAX);
printf("long double %d bytes %E to %E |>(using %%E as format specifier)\n",sizeof(long double),LDBL_MIN,LDBL_MAX);
printf("long double %d bytes %e to %e |>(using %%e as format specifier)\n",sizeof(long double),LDBL_MIN,LDBL_MAX);
printf("long double %d bytes %llf to %llf |>(using %%llF as format specifier)\n",sizeof(long double),LDBL_MIN,LDBL_MAX);
printf("long double %d bytes %le to %le |>(using %%le as format specifier)\n",sizeof(long double),LDBL_MIN,LDBL_MAX);
printf("long double %d bytes %f to %f |>(using %%f as format specifier)\n",sizeof(long double),LDBL_MIN,LDBL_MAX);
printf("-------------------------------------------------------------\n");
printf("NOTE: I'm trying to find the range of long double!\n");
printf(" But none of the format specifiers seems to work! Please Help...\n");
return 0;
}

但是输出是: screenshot

根据https://www.tutorialspoint.com/cprogramming/c_data_types.htm

long double 的内存大小:10 个字节(我知道这可能会有所不同)

长 double 值的范围:3.4E-4932 到 1.1E+4932

*但是我的代码的输出与 3.4E-4932 到 1.1E+4932 相差甚远。但为什么呢?

最佳答案

编译器警告未完全启用。

许多编译器指出 printf() 说明符不匹配

<小时/>

说明符与long doublesize_t(sizeof的返回类型)不匹配@melpomene

L Specifies that a following a, A, e, E, f, F, g, or G conversion specifier applies to a long double argument. C11dr §7.21.6.1 7

// wrong specifiers
printf("long double %d bytes %E to %E |>(using %%E as format specifier)\n",
sizeof(long double),LDBL_MIN,LDBL_MAX);
// Correct specifiers
printf("long double %zu bytes %LE to %LE |>(using %%E as format specifier)\n",
sizeof(long double),LDBL_MIN,LDBL_MAX);

关于c - 如何在C中打印long double的范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42472724/

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com