我正在使用 Windows 8.1 64位。
IDE: Eclipse Luna(4.4.0)。
并使用 MinGW v4.9.1
我在eclipse中输入了这个程序,但它显示的是在long double中显示零。我尝试了%llf和%Lf,并尝试了类型转换,但没有任何方法可以帮助我。
1. int main(void) {
2. float floatNumber;
3. double doubleNumber;
4. long double longDoubleNumber;
5.
6. // Size of floating point types
7. printf("Storage size for unsigned float : %lu bytes \n", sizeof(float));
8. printf("Storage size for double : %lu bytes \n", sizeof(double));
9. printf("Storage size for long double : %lu bytes \n", sizeof(long double));
10.
11. floatNumber = 2.0/3.0;
12. doubleNumber = 2.0/3.0;
13. longDoubleNumber = 2.0/3.0;
14.
15. puts("\nCompare precision at 4 decimal points:");
16. printf("floatNumber = %1.4f\n", floatNumber);
17. printf("doubleNumber = %1.4lf\n", doubleNumber);
18. printf("longDoubleNumber = %1.4Lf\n", longDoubleNumber);
19.
20. puts("\nCompare precision at 10 decimal points:");
21. printf("floatNumber = %1.10f\n", floatNumber);
22. printf("doubleNumber = %1.10lf\n", doubleNumber);
23. printf("longDoubleNumber = %1.10Lf\n", longDoubleNumber);
24.
25. puts("\nCompare precision at 30 decimal points:");
26. printf("floatNumber = %1.30f\n", floatNumber);
27. printf("doubleNumber = %1.50lf\n", doubleNumber);
28. printf("longDoubleNumber = %1.50Lf\n", longDoubleNumber);
29. return 0;
30. }
这是输出: -
Floating point type usage in C:
Storage size for unsigned float : 4 bytes
Storage size for double : 8 bytes
Storage size for long double : 16 bytes
Compare precision at 4 decimal points:
floatNumber = 0.6667
doubleNumber = 0.6667
longDoubleNumber = 0.0000
Compare precision at 10 decimal points:
floatNumber = 0.6666666865
doubleNumber = 0.6666666667
longDoubleNumber = 0.0000000000
Compare precision at 30 decimal points:
floatNumber = 0.666666686534881590000000000000
doubleNumber = 0.66666666666666663000000000000000000000000000000000
longDoubleNumber = 0.00000000000000000000000000000000000000000000000000
当我尝试这个时:
printf("Storage size for unsigned float : %zu bytes \n", sizeof floatNumber);
printf("Storage size for double : %zu bytes \n", sizeof doubleNumber);
printf("Storage size for long double : %zu bytes \n", sizeof longDoubleNumber);
输出是:
Floating point type usage in C:
Storage size for unsigned float : zu bytes
Storage size for double : zu bytes
Storage size for long double : zu bytes
其他一切与上述相同。
Eclipse显示的警告列表是: -
line 7,8,9,18,23,28:- too many arguments for format [-Wformat-extra-args]
line 18,23,28:- unknown conversion type character 'L' in format [-Wformat=]
line 7,8,9:- format '%lu' expects argument of type 'long unsigned int', but argument
2 has type 'long long unsigned int' [-Wformat=]
{if i use %zu instead of %lu}
line 7,8,9:- unknown conversion type character 'z' in format [-Wformat=]
long double的格式说明符是什么?
答案 0 :(得分:0)
size_t
是类型sizeof()
返回,因此请使用zu
格式说明符。使用错误的格式说明符会导致未定义的行为。
printf("Storage size for unsigned float : %zu bytes \n", sizeof(float));
注意:%zd
适用于已签名的ssize_t