在(好的)旧 printf 的上下文中,长度子说明符“l”有什么影响吗?
Is there any effect of the length sub-specifier “l“ in the context of the (good) old printf?
问题是关于(好的)旧 printf 函数。
长度子说明符“l”记录在 table 名为“specifiers”的文件中:
https://www.cplusplus.com/reference/cstdio/printf/
用于解释长度子说明符“l”与说明符“f F e E g G a A”组合的单元格留空。这是否意味着,例如“%le”的行为总是像“%e”?
我努力寻找一个例子,让我在“%le”-输出和“%e”-输出之间找到差异……但没有成功。难道“%le”和“%e”在 printf 上下文中通常产生相同的输出吗?我正在使用 Visual Studio 版本 16.11.5 和 MSC 版本 19.29.30136(MSC_FULL_VER 是“192930136”)。这是我的代码:
#include <iostream>
//to get the MSC-compiler version
#define XSTR(x) STR(x)
#define STR(x) #x
#pragma message("_MSC_FULL_VER:")
#pragma message(XSTR(_MSC_FULL_VER))
int main()
{
double d = 1.123456789123456789123456789;
long double ld = static_cast<long double>(d);
printf("%.20e\n" , d);
printf("%.20le\n", d);
printf("%.20le\n", ld);
printf("%.20e\n" , ld);
return 0;
}
这是相应的输出:
1.12345678912345681155e+00
1.12345678912345681155e+00
1.12345678912345681155e+00
1.12345678912345681155e+00
2018 C 标准在第 7.21.6.1 条第 7 段中说 l
:
… has no effect on a following a
, A
, e
, E
, f
, F
, g
, or G
conversion specifier.
(上面使用的省略“...”的文本指定了 l
对其他说明符的影响。)
正如 Eric Postpischil 已经指出的那样,长度说明符 l
对说明符 f F e E g G a A
没有影响。使用长度说明符 L
打印 long double
.
请注意,某些编译器(例如 MSVC)会将 long double
视为 double
。
如果编译器支持 long double
的更高进动,则以下代码将生成不同的输出:
#include <stdio.h>
int main()
{
double d = 1.123456789123456789123456789;
long double ld = 1.123456789123456789123456789L;
printf("Double: %.20e\n", d);
printf("Long Double: %.20Le\n", ld);
printf("Size of double: %lu\n", sizeof(double));
printf("Size of long double: %lu\n", sizeof(long double));
}
clang 13.0.0 的输出如下:
Double: 1.12345678912345681155e+00
Long Double: 1.12345678912345678911e+00
Size of double: 8
Size of long double: 16
问题是关于(好的)旧 printf 函数。 长度子说明符“l”记录在 table 名为“specifiers”的文件中:
https://www.cplusplus.com/reference/cstdio/printf/
用于解释长度子说明符“l”与说明符“f F e E g G a A”组合的单元格留空。这是否意味着,例如“%le”的行为总是像“%e”? 我努力寻找一个例子,让我在“%le”-输出和“%e”-输出之间找到差异……但没有成功。难道“%le”和“%e”在 printf 上下文中通常产生相同的输出吗?我正在使用 Visual Studio 版本 16.11.5 和 MSC 版本 19.29.30136(MSC_FULL_VER 是“192930136”)。这是我的代码:
#include <iostream>
//to get the MSC-compiler version
#define XSTR(x) STR(x)
#define STR(x) #x
#pragma message("_MSC_FULL_VER:")
#pragma message(XSTR(_MSC_FULL_VER))
int main()
{
double d = 1.123456789123456789123456789;
long double ld = static_cast<long double>(d);
printf("%.20e\n" , d);
printf("%.20le\n", d);
printf("%.20le\n", ld);
printf("%.20e\n" , ld);
return 0;
}
这是相应的输出:
1.12345678912345681155e+00
1.12345678912345681155e+00
1.12345678912345681155e+00
1.12345678912345681155e+00
2018 C 标准在第 7.21.6.1 条第 7 段中说 l
:
… has no effect on a following
a
,A
,e
,E
,f
,F
,g
, orG
conversion specifier.
(上面使用的省略“...”的文本指定了 l
对其他说明符的影响。)
正如 Eric Postpischil 已经指出的那样,长度说明符 l
对说明符 f F e E g G a A
没有影响。使用长度说明符 L
打印 long double
.
请注意,某些编译器(例如 MSVC)会将 long double
视为 double
。
如果编译器支持 long double
的更高进动,则以下代码将生成不同的输出:
#include <stdio.h>
int main()
{
double d = 1.123456789123456789123456789;
long double ld = 1.123456789123456789123456789L;
printf("Double: %.20e\n", d);
printf("Long Double: %.20Le\n", ld);
printf("Size of double: %lu\n", sizeof(double));
printf("Size of long double: %lu\n", sizeof(long double));
}
clang 13.0.0 的输出如下:
Double: 1.12345678912345681155e+00
Long Double: 1.12345678912345678911e+00
Size of double: 8
Size of long double: 16