我使用<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/