gpt4 book ai didi

c - scanf 的四元数和参数类型

转载 作者:行者123 更新时间:2023-12-02 04:36:36 27 4
gpt4 key购买 nike

我使用 <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 number 转换成字符串,然后使用 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/

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