quadmath and argument type for scanf

删除回忆录丶 提交于 2019-12-06 08:09:50

scanf (and related functions and printf and related functions) is not extensible. It can only parse what the standard library knows about. The C standard library comes with the operating system, not the compiler. It does not know about libquadmath, which is a compiler extension.

So you'll have to read strings and convert them separately using strtoflt128.

Note that C++ streams can be extended to extract __float128, but I don't see C++ interface in the quadmath library.

You can't use %Qf specifier in the scanf, you should input the high precision numbers as string, then use strtoflt128 to convert. You also can't use %Qf specifier in the printf, use quadmath_snprintf to converts a __float128 floating-point number into a string, then use printf with %s specifier to display it. Here is an example.

#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;
}

run the program:

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!