Format specifier %Lf is giving errors for `long double` variables

家住魔仙堡 提交于 2020-06-28 03:25:39

问题


I am getting the following errors:

In function 'main':
[Warning] unknown conversion type character 'L' in format [-Wformat=]
[Warning] too many arguments for format [-Wformat-extra-args]

In function 'error_user':
[Warning] unknown conversion type character 'L' in format [-Wformat=]
[Warning] too many arguments for format [-Wformat-extra-args]

In the below code:

#include <stdio.h>
#include <stdlib.h>

void error_user (long double *error);

int main(void)
{
    long double error;

    printf("What error do you want?\n");

    error_user (&error);

    printf("%Lf\n", error);

    return 0;
}

void error_user (long double *error)
{
    scanf("%Lf", error);
}

As far as I know the format specifier of a long double is %Lf so not really sure how to solve this one. Thank you!

Compiled with TDM-GCC 4.9.2 64-bit Release in DEV-C++.


回答1:


Your compiler doesn't recognize %Lf , you need to provide the compiler flag -D__USE_MINGW_ANSI_STDIO=1

Example:

$ gcc filename.c -Wall -Wextra -pedantic -O3 -D__USE_MINGW_ANSI_STDIO=1
                                             ^^^^^^^^^^^^^^^^^^^^^^^^^^

As you are using Dev-C++, you should probably also add -std=c11 flag to enable C11 standard.

This thread explains how you should add flags to Dev-C++:

How to change mode from c++98 mode in Dev-C++ to a mode that supports C++0x (range based for)?

So you need to add the flags -std=c11 and -D__USE_MINGW_ANSI_STDIO=1 using the instructions in the linked thread.

Since Dev-C++ uses an older standard, it's possible that adding only -std=c11 can solve the issue. Try it first.



来源:https://stackoverflow.com/questions/62359775/format-specifier-lf-is-giving-errors-for-long-double-variables

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