c - scanf 的四边形和参数类型

标签 c scanf quadruple-precision

我使用<quadmath.h> 。使用哪种参数类型可以正确读取我的输入? 如果我使用 double 它看起来像:

printf("enter 3 values for s, t and mt:\n");
scanf("%lf %lf %lf", &s, &t, &mt);
printf("%lf %lf %lf\n", s, t, mt);

我尝试了不同的可能性,而不是“l”,例如:

scanf("%Qf %Qf %Qf", &s, &t, &mt);

或者甚至没有

 scanf("%f %f %f", &s, &t, &mt);

但是我收到错误。

最佳答案

scanf中不能使用%Qf说明符,应将高精度数字作为字符串输入,然后使用strtoflt128转变。您也不能在 printf 中使用 %Qf 说明符,请使用 quadmath_snprintf 转换 __float128 float 字转换为字符串,然后使用 printf%s 说明符来显示它。这是一个例子。

#include <stdio.h>
#include <quadmath.h>
int main()
{
        char s[256], t[256], mt[256];
        printf("enter 3 values for s, t and mt:\n");
        scanf("%s %s %s", s, t, mt);
        __float128 qs = strtoflt128(s, NULL);

        __float128 qt = strtoflt128(t, NULL);

        __float128 qmt = strtoflt128(mt, NULL);

        quadmath_snprintf(s, 256, "%Qf", qs);

        quadmath_snprintf(t, 256, "%Qf", qt);

        quadmath_snprintf(mt, 256, "%Qf", qmt);

        printf("%s %s %s", s, t, mt);
        return 0;
}

运行程序:

enter 3 values for s, t and mt:
3.4 4.5 5.6(enter)
3.400000 4.500000 5.600000

关于c - scanf 的四边形和参数类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21847735/

相关文章:

c - 如何使用 fscanf 进行条件解析?

c - for 循环接受比循环条件多一个值

fortran - 16 字节实数的 MPI_AllReduce 的奇怪结果

c++ - 为什么优化标志 (-O3) 不能加速四倍精度计算?

c++ - 安全蓄能器真的这么复杂吗?

c - C:如何加载,存储和打印字符串和整数的混合矩阵

使用 stat 函数检查 C 中是否存在并且可执行

c++ - Pthreads——主线程和其他线程

c - scanf 不会让我离开循环 — 知道为什么以及如何解决它吗?