Nethrenial
Nethrenial

Reputation: 728

Why Long double data type is working strange in my C code?

I'm using Windows 10 and MinGW GCC compiler(not the mingw64 one), but when I try this code, This is my GCC version,

C:\Users\94768>gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/6.3.0/lto-wrapper.exe
Target: mingw32
Configured with: ../src/gcc-6.3.0/configure --build=x86_64-pc-linux-gnu --host=mingw32 --target=mingw32 --with-gmp=/mingw --with-mpfr --with-mpc=/mingw --with-isl=/mingw --prefix=/mingw --disable-win32-registry --with-arch=i586 --with-tune=generic --enable-languages=c,c++,objc,obj-c++,fortran,ada --with-pkgversion='MinGW.org GCC-6.3.0-1' --enable-static --enable-shared --enable-threads --with-dwarf2 --disable-sjlj-exceptions --enable-version-specific-runtime-libs --with-libiconv-prefix=/mingw --with-libintl-prefix=/mingw --enable-libstdcxx-debug --enable-libgomp --disable-libvtv --enable-nls
Thread model: win32
gcc version 6.3.0 (MinGW.org GCC-6.3.0-1)

The code is,

 #include <stdio.h>

 void main() {

    float a = 1.12345;
    double b = 1.12345;
    long double c = 1.12345;
    printf("float value is is %f\n", a);
    printf("double value is %lf\n", b);
    printf("long double value is %Lf\n", c);
}

I got this output which is not what I expected, I don't understand what's the issue here, I am an absolute beginner to C programming.

float value is 1.123450
double value is 1.123450
long double value is -0.000000 

Immensely appreciate your guidance!

Upvotes: 2

Views: 494

Answers (2)

Cezary
Cezary

Reputation: 84

It looks like GCC on Windows doesn't work well with the long double type unless you use the compiler flag:

-D__USE_MINGW_ANSI_STDIO

So, if your source file is, e.g., code.c, then you need to run:

gcc -D__USE_MINGW_ANSI_STDIO code.c -o code.exe

You may also refer to the original forum thread where I found the information when came across the same issue: cprogramming forum

Upvotes: 0

Brecht Sanders
Brecht Sanders

Reputation: 7287

GCC 6.3? That's ancient! Latest GCC release at the time of this writing is 11.1.

I recommend you move away from MinGW and switch to MinGW-w64 as it is much more up to date (and supports both 32-bit and 64-bit Windows).

You can get a recent standalone MinGW-w64 build from https://winlibs.com/

Built with that compiler the output of your code is:

float value is is 1.123450
double value is 1.123450
long double value is 1.123450

Upvotes: 1

Related Questions