关于"long double"的困惑
按照C Primer Plus中文第五版中的描述,long double 在使用scanf()函数读取时,应采用“%Lf" "%Le" "%Lg"作为控制字符串.
如下代码分别在VC 2010 Express 、MinGW(gcc version 4.8.1 (tdm-2))和QT5.2.1 for Desktop MinGW4.8 32bit 三种环境下测试。
#include <stdio.h> int main(void) { long double x_longdouble; scanf("%Lf", &x_longdouble); printf("%Le", x_longdouble); getchar(); getchar(); return 0; }
VC :能够编译并输入输出。
MinGW 和 QT:编译警报:
unknown conversion type character 'L' in format [-Wformat=] scanf("%Lf", &x_longdouble); unknown conversion type character 'L' in format [-Wformat=] printf("%Le", x_longdouble);
输入: 0.234
VC:
0.234 2.340000e-001
MinGW:
0.234 0.000000e+000
QT:
0.234 2.340000e-001
google之后,找到一篇文章。
http://bytes.com/topic/c/answers/135253-printing-long-double-type-via-printf-mingw-g-3-2-3-a
“MINGW is broken regarding long double. I had reported this myself some time ago, and in summary here is what is going on: In Windows world double and long double is the same. However in MINGW long double is larger than double, however (I do not know more specifics) it can print only the long double of MS Windows, that is the double. They are using an MS library or something. You have two options. Either stick to double which is the same in Windows world, or use DJGPP which is another GCC port (but creates 32-bit DOS executables).
Or use some other compiler.”
附:
查看MinGW中gcc版本的方法:通过power shell进入gcc所在目录(...\MinGW\bin)执行命令:.\gcc -v