I'm just starting to learn C moving from C++, and I was just trying out a ton of variable types. I am using the MingGW-w64 toolset with the GCC compiler. This version supposedly uses UCRT runtime instead of MSVCRT. I've heard long doubles are the same as doubles using MSVCRT, but UCRT appears to use 128 bits for long double and 64 for doubles. However, I am having a very strange issue with long doubles. I am using the correct "%Lg" for printf according to Google. I have tried "%llf," "%Lf," and "%Le," but they all return the same. I first defined PI as #define M_PI 3.1415926535897932384626433832795028841971693993751L since "math.h" does not appear to have M_PI defined. Casting it to a double appears to return the correct value, but the long double value is extremely small. What's going on? I realize a double will be good enough, but I am just wondering what the issue is.
I realize I don't need to create a const variable for the define. I was just messing around.
The only mention I can find is that long double is broken in MinGW, but that is for the MSVCRT version, not UCRT.
#include <stdio.h>
#include <math.h>
#define M_PI 3.1415926535897932384626433832795028841971693993751L
int main(void) {
const long double PI = M_PI;
printf("Size of double: %d\n", (int)sizeof(double));
printf("Size of long double: %d\n", (int)sizeof(long double));
printf("PI == %.6Lg\n", PI);
return 0;
}
Output:
Size of double: 8
Size of long double: 16
PI == 3.10817e-317
The tasks.json file I am using in Visual Studio Code is as follows. Note that I am a newb at compiling like this, so I may have made some mistakes.
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe build active file",
"command": "C:\\mingw64\\bin\\gcc.exe",
"args": [
"-m64",
"-std=c11",
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: C:\\mingw64\\bin\\gcc.exe"
}
]
}