Search code examples
cformatdev-c++long-doubletdm-gcc

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


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++.


Solution

  • 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.